8.1 shell introduction
What is shell?
- The shell is a command interpreter that provides interaction between users and machines (command line terminals)
- A script shell is a manifestation
- The last section of passwd is the user's shell
- shell supports specific grammars, such as logical judgement, loops
- Each user can have his or her own specific shell
- The default shell for CentOS 7 is bash (Bourne Again Shell)
- And zsh ksh and so on. Generally the same, the details are different.
- No installation by default
[root@lixiang01 ~]# yum list |grep zsh autojump-zsh.noarch 22.3.0-3.el7 epel zsh.x86_64 5.0.2-25.el7_3.1 updates zsh-html.x86_64 5.0.2-25.el7_3.1 updates zsh-lovers.noarch 0.9.0-1.el7 epel [root@lixiang01 ~]# yum list |grep ksh ksh.x86_64 20120801-26.el7 base mksh.x86_64 46-5.el7 base
8.2 Order History
[root@ax-01 ~]# history
- History command, record history command
- bash_history in the user's home directory, recording the command history after the last normal exit
- Abnormal exit incomplete preservation
Modify the number of command history saved
[root@ax-01 ~]# echo $HISTSIZE // Maximum default command history 1000 1000 [root@ax-01 ~]# Vim/etc/profile//where you can adjust environmental variables
[root@ax-01 ~]# source !$ source /etc/profile [root@ax-01 ~]# echo $HISTSIZE 5000
- The environment variable controls $HISTSIZE to save the maximum save command. Runtime memory can be saved more than 1000 pieces
- History-c clears the command history in memory
- Save to. bash_history after exiting the terminal
Modify the command history display format
[root@ax-01 ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S" [root@ax-01 ~]# history 2 2017/08/30 09:51:25echo $HISTSIZE 3 2017/08/30 09:51:44vim /etc/profile 4 2017/08/30 09:53:37echo $HISTSIZE [root@ax-01 ~]# !vim vim /etc/profile
[root@ax-01 ~]# source /etc/profile [root@ax-01 ~]# !echo echo $HISTTIMEFORMAT %Y/%m/%d %H:%M:%S
- HISTTIMEFORMAT="%Y/%m/%d%% H:%M:%S"// / Note that the case modification profile is permanently valid
Permanent Preservation of Order History
[root@ax-01 ~]# chattr +a .bash_history [root@ax-01 ~]# lsattr .bash_history -----a-------e-- .bash_history
- Not affected by environmental variable HISTSIZE
Common usage of exclamation marks
[root@ax-01 etc]# cd /etc/yum.repos.d/ [root@ax-01 yum.repos.d]# cd .. [root@ax-01 etc]# !! Execute the last command again. cd .. [root@ax-01 /]# [root@ax-01 /]# cd [root@ax-01 ~]# history | tail -n4 1051 cd /etc/yum.repos.d/ 1052 cd .. 1053 cd 1054 history | tail -n4 [root@ax-01 ~]# ! 1051 // / Run as history number cd /etc/yum.repos.d/
- !!// Last command
- ! n // n is the number that runs according to history number
- xx // Execute the last command that started with xx characters
8.3 Command Completion and Aliases
- tab command completion,
- Knock and auto-complete if subsequent characters are unique
- If it's not unique, tap twice to see the optional follow-up characters
Parameter Completion,
[root@ax-01 ~]# Yum install-y bash-completion// installation parameter completion package, need to restart [root@ax-01 ~]# reboot [root@ax-01 ~]# systemctl restart network network-online.target network.service
- Completion of support parameters newly added to CentOS 7. Very practical
Alias alias
[root@ax-01 ~]# alias renet='systemctl restart network' [root@ax-01 ~]# echo "alias renet='systemctl restart network'" >> .bashrc [root@ax-01 ~]# unalias renet cancelled [root@ax-01 ~]# Vim. bashrc deletes definition aliases
- Some system aliases are in files in the / etc/profile.d / directory
- Each user has its own alias profile
8.4 wildcards
- ls *.txt // / denotes arbitrary
- Ls?. TXT // / denotes any one
- ls [0-9].txt// any one in the range of representation
- ls {1,3}.txt // / ibid.
Experiment
[root@lixiang01 ~]# ls *txt* 1_heard.txt.bak 1.txt 22.txt 2.txt.bak test.txt word1.txt Record.txt [root@lixiang01 ~]# ls 1* 123.tar.bz2 1_heard.txt.bak 1.txt 111: 22.txt [root@lixiang01 ~]# ls ?.txt 1.txt [root@lixiang01 ~]# touch 2.txt [root@lixiang01 ~]# touch 3.txt [root@lixiang01 ~]# ls ?.txt 1.txt 2.txt 3.txt [root@lixiang01 ~]# ls [0-3].txt 1.txt 2.txt 3.txt [root@lixiang01 ~]# ls [123].txt 1.txt 2.txt 3.txt [root@lixiang01 ~]# ls [0-9].txt 1.txt 2.txt 3.txt [root@lixiang01 ~]# ls [0-9a-zA-Z].txt 1.txt 2.txt 3.txt [root@lixiang01 ~]# touch d.txt [root@lixiang01 ~]# ls [0-9a-zA-Z].txt 1.txt 2.txt 3.txt d.txt [root@lixiang01 ~]# ls {1,3,d}.txt 1.txt 3.txt d.txt
8.5 Input and output redirection
> Output redirection "Append" 2 > Error redirection, support additional 2 > &> Full redirection, support for additions &>>
Distinguish between correct and wrong output information
[root@ax-01 ~]# cat /etc/passwd nosuchthing.txt > right.log 2> err.log [root@ax-01 ~]# cat err.log cat: nosuchthing.txt: No such file or directory