8.1 Shell Introduction: Interaction between the converters
1. Shell is a command interpreter that provides interaction between the user and the machine
2, support specific grammar, such as logical judgment, loop
3, each user can have their own specific shell
4. CentOS7 default shell is bash (Bourne Agin Shell)
5, there are zsh, ksh, etc.
See if the system has zsh and Ksh installed, as shown in the following example:
[[email protected] ~]# yum list |grep zsh
zsh.x86_64 5.0.2-25.el7 installed
autojump-zsh.noarch 22.3.0-3.el7 epel
zsh.x86_64 5.0.2-28.el7 base
zsh-html.x86_64 5.0.2-28.el7 base
zsh-lovers.noarch 0.9.0-1.el7 epel
[[email protected] ~]# yum list |grep ksh
ksh.x86_64 20120801-34.el7 base
mksh.x86_64 46-5.el7 base
python-XStatic-Rickshaw.noarch 1.5.0.0-4.el7 epel
python-moksha-common.noarch 1.2.3-2.el7 epel
python-moksha-hub.noarch 1.5.3-2.el7 epel
python-moksha-wsgi.noarch 1.2.2-2.el7 epel
8.2 Command history:
1. History command//View previous commands used.
2. Bash_history//Store used command files in Root's home directory
3, maximum 1000//storage use the maximum number of commands
4. Variable Histsize
5,/etc/profile in the revision
6, histtimeformat= "%y/%m/%d%h:%m:%s"
7, permanent preservation chattr +a ~/.bash_history
8 、!! Executes the previous command
9,!n//n For example:!176 so it executes 176 of this command in the history of the command.
10,!echo//echo said you need to find the command history from the bottom up look for the first command that begins with Word to execute it.
Examples are as follows:
[[email protected] ~] # ls /root/.bash_history
/root/.bash_history
[[email protected] ~] # cat /root/.bash_history
[[email protected] ~] # history
1 ls -l / tmp /
2 ls -l
...
585 ls /root/.bash_history
586 cat /root/.bash_history
587 history
[[email protected] ~] # echo $ HISTSIZE
1000
[[email protected] ~] # history -c // Clear the current command history
[[email protected] ~] # history
1 history
[[email protected] ~] # cat .bash_history // Check whether the configuration file command is cleared. If not, only the current command history can be cleared.
ls -l / tmp /
ls -l
which rm
Note: The currently used commands are not saved directly to the. Bash_history configuration file, and are only saved to the configuration file when you exit the terminal.
To change the value of a command history variable:
[[email protected] ~]# vi/etc/profile//Change store command variable, enter this file to find the location change
650) this.width=650; "title=" 4.png "alt=" 4008d7031d0f8f2eb4dc7a6f8a3d0db1.png-wh_ "src=" https://s5.51cto.com/oss/ 201711/15/4008d7031d0f8f2eb4dc7a6f8a3d0db1.png-wh_500x0-wm_3-wmp_4-s_4237233340.png "/>
[[email protected] ~] # echo $ HISTSIZE
1000
[[email protected] ~] # source / etc / profile // Change the variable to execute this command
[[email protected] ~] # echo $ HISTSIZE // Complete the change
5000
[[email protected] ~] # history
1 history
2 cat .bash_history
3 vi / etc / profile
4 echo $ HISTSIZE
5 source / etc / profile
6 echo $ HISTSIZE
7 history
To change the command history format:
[[email protected] ~] # HISTTIMEFORMAT = "% Y /% m /% d% H:% M:% S"
[[email protected] ~] # echo $ HISTTIMEFORMAT
% Y /% m /% d% H:% M:% S
[[email protected] ~] # history
1 2017/11/14 16:45:35 history
2 2017/11/14 16:46:43 cat .bash_history
3 2017/11/14 17:59:55 vi / etc / profile
4 2017/11/14 18:04:45 echo $ HISTSIZE
5 2017/11/14 18:05:12 source / etc / profile
6 2017/11/14 18:05:14 echo $ HISTSIZE
7 2017/11/14 18:06:30 history
8 2017/11/14 18:07:10 HISTTIMEFORMAT = "% Y /% m /% d% H:% M:% S"
9 2017/11/14 18:07:34 echo $ HISTTIMEFORMAT
10 2017/11/14 18:07:57 history
[[email protected] ~] # vim / etc / profile // If you want to save the history effect, enter the configuration file and add the following variable command, save and exit.
650) this.width=650; "title=" 5.png "alt=" 90633b96be853f0dbdb85a677a3aa8f1.png-wh_ "src=" https://s4.51cto.com/oss/ 201711/15/90633b96be853f0dbdb85a677a3aa8f1.png-wh_500x0-wm_3-wmp_4-s_2751719141.png "/>
[[email protected] ~] # source / etc / profile
[[email protected] ~] # echo $ HISTTIMEFORMAT // This will save even if you open another terminal.
% Y /% m /% d% H:% M:% S
In order for the command history to be permanently saved, and not to let others destroy it, you can give it a special permission.
[[email protected] ~]# chattr +a ~/.bash_history//Run this command.
Note: If you do not exit the terminal properly after executing this command, you will cause the command not to be saved completely.
8.3 Command completion and aliases:
1, Tab key, knock, knock two
2, parameter completion, installation Bash-completion
3. Alias aliases give the command a name again
4, each user has their own configuration alias file ~/.BASHRC//store alias path address
5. ls/etc/profile.d///The path address where the alias is stored
6. Custom alias placed to ~/.BASHRC//custom aliases stored address
Examples are as follows:
[[email protected] ~] # systemctl restart network // Use tab to complete this command parameter
[[email protected] ~] # yum install -y bash-completion // Install this library
[[email protected] ~] # reboot // Restart the system
[[email protected] ~] # alias restartnet = ‘systemctl restart network.service’ // alias renames the command
[[email protected] ~] # restartnet // The set alias command takes effect
[[email protected] ~] # alias // View existing aliases
alias cp = ‘cp -i’
alias egrep = ‘egrep --color = auto’
alias fgrep = ‘fgrep --color = auto’
alias grep = ‘grep --color = auto’
alias l. = ‘ls -d. * --color = auto’
alias ll = ‘ls -l --color = auto’
alias ls = ‘ls --color = auto’
alias mv = ‘mv -i’
alias restartnet = ‘systemctl restart network.service’
alias rm = ‘rm -i’
alias which = ‘alias | / usr / bin / which --tty-only --read-alias --show-dot --show-tilde’
[[email protected] ~] # unalias restartnet // Cancel the set alias
[[email protected] ~] # restartnet // The alias command is invalid
-bash: restartnet: command not found
8.4 Wildcard characters:
1, LS *.txt//means to find a. txt wildcard file
2, ls?. TXT//denotes an arbitrary character
3, LS [0-9].txt//list files that meet the conditions
4, ls {1,2}.txt//Use curly braces to list the files you need
Examples are as follows:
[[email protected] ~]# ls
111 1_heard.txt.bak 1.txt.bak 2.txt 456 aminglinux
123 1_sorft.txt.bak 234 3.txt aming2 anaconda-ks.cfg
[[email protected] ~]# ls *.txt
2.txt 3.txt
[[email protected] ~]# ls *txt
2.txt 3.txt
[[email protected] ~]# ls *txt*
1_heard.txt.bak 1_sorft.txt.bak 1.txt.bak 2.txt 3.txt
[[email protected] ~]# ls 1*
1_heard.txt.bak 1_sorft.txt.bak 1.txt.bak
111:
123:
[[email protected] ~]# ls ?.txt
2.txt 3.txt
[[email protected] ~]# touch 1.txt
[[email protected] ~]# ls ?.txt
1.txt 2.txt 3.txt
[[email protected] ~]# touch a.txt
[[email protected] ~]# touch bb.txt
[[email protected] ~]# ls ?.txt
1.txt 2.txt 3.txt a.txt
[[email protected] ~]# ls [0-3].txt
1.txt 2.txt 3.txt
[[email protected] ~]# ls [12].txt
1.txt 2.txt
[[email protected] ~]# ls [23].txt
2.txt 3.txt
[[email protected] ~]# ls {1,2}.txt
1.txt 2.txt
8.5 Input and output redirection
1. Cat 1.txt >2.txt//> It will output the correct information generated by the command to the specified file, delete the previous file content rewrite.
2, Cat 1.txt >> 2.txt//>> Append the previous command output redirect to the following command, do not delete the old.
3, LS aaa.txt 2> err//It will output the error message generated by the command to the specified file.
4, LS aaa.txt 2>> err//error message output append redirect
5, Wc-l < 1.txt//view the number of rows in a file
Examples are as follows:
[[email protected] ~] # lsaaa // Output an error message
-bash: lsaaa: command not found
[[email protected] ~] # lsaaa 2> a.txt // Save a message to the specified file
[[email protected] ~] # cat a.txt // View results
-bash: lsaaa: command not found
[[email protected] ~] # lsaaa 2 >> a.txt // Append an error message to the file
[[email protected] ~] # cat a.txt // View results
-bash: lsaaa: command not found
-bash: lsaaa: command not found
[[email protected] ~] # ls [12] .txt aaa.txt &> a.txt // &> Redirect to a file with correct error information
[[email protected] ~] # cat a.txt // View results
ls: cannot access aaa.txt: no such file or directory
1.txt
2.txt
[[email protected] ~] # ls [12] .txt aaa.txt & >> a.txt // append the correct error message redirect
[[email protected] ~] # cat a.txt
ls: cannot access aaa.txt: no such file or directory
1.txt
2.txt
ls: cannot access aaa.txt: no such file or directory
1.txt
2.txt
[[email protected] ~] # ls [12] .txt aaa.txt> 1.txt 2> a.txt // There are both correct and incorrect outputs, and they are saved separately in the specified file.
[[email protected] ~] # cat 1.txt
1.txt
2.txt
[[email protected] ~] # cat a.txt
ls: cannot access aaa.txt: no such file or directory
This article is from the "Gary Blog" blog, please be sure to keep this source http://taoxie.blog.51cto.com/10245493/1982118
Introduction to the Shell Basics of Linux (1)