8.1 shell介紹
·什麼是shell?
- shell是一個命令解釋器,提供用戶和機器之間的交互
- 支持特定語法,比如邏輯判斷、循環
- 每個用戶都可以有自己特定的shell
- CentOS7默認shell爲bash(Bourne Agin Shell)
- 還有zsh、ksh等
8.2 命令歷史
方向鍵 ↑ 可以查看之前用過的命令,命令存放在家目錄 ~/.bash_history
history 查看之前使用過的所有命令
[root@arslinux-01 ~]# history
命令歷史最大可保存1000條,由環境變量 $HISTSIZE 定義
[root@arslinux-01 ~]# echo $HISTSIZE 1000
·執行的命令,並不是實時寫入到 bash_history 中,而是暫時存放在內存中,當退出終端時,才存入
history -c 清空當前內存命令歷史 ,但是無法清空配置文件bash_history
/etc/profile 中定義環境變量 $HISTSIZE
修改 HISTSIZE 的數值,可以更改命令歷史最大保存的數量
想要 HISTSIZE 立刻生效,需要重新進一下終端,或者執行source /etc/profile
[root@arslinux-01 ~]# vim /etc/profile [root@arslinux-01 ~]# echo $HISTSIZE 1000 [root@arslinux-01 ~]# source /etc/profile [root@arslinux-01 ~]# echo $HISTSIZE 5000
·改變命令歷史格式:改變環境變量 HISTTIMEFORMAT
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
[root@arslinux-01 ~]# history
想要永久生效,可以寫入到 /etc/profie 中去
[root@arslinux-01 ~]# vim /etc/profile [root@arslinux-01 ~]# source /etc/profile [root@arslinux-01 ~]# echo $HISTTIMEFORMAT %Y/%m/%d %H:%M:%S
永久保存 chattr +a ~/.bash_history,只能追加,不能刪除
運行上一條命令 !!
運行第n條命令 !n
從最後倒着去找以word開頭的命令 !word
8.3 命令補全和別名
Tab鍵,敲一下,敲兩下
如果有一個相同開頭,按一下就會出現;如果有多個相同開頭,需要敲兩下
CentOS6中只支持命令補全,參數無法補全;CentOS7中支持參數補全
默認不支持參數補全,需要安裝bash-completion
[root@arslinux-01 ~]# yum install -y bash-completion
重啓系統後生效
alias 別名=‘命令’ 別名 alias別名給命令重新起個名字
[root@arslinux-01 ~]# alias renet="systemctl restart network.service" [root@arslinux-01 ~]# alias 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 renet='systemctl restart network.service' alias rm='rm -i' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
·每個用戶都有自己配置別名的文件 ~/.bashrc
·其餘別名存放在 /etc/profile.d/ 中
[root@arslinux-01 ~]# ls /etc/profile.d/ 256term.csh bash_completion.sh colorgrep.sh colorls.sh lang.csh less.csh sh.local vim.sh which2.sh 256term.sh colorgrep.csh colorls.csh csh.local lang.sh less.sh vim.csh which2.csh [root@arslinux-01 ~]# vim /etc/profile.d/colorgrep.sh
unalias 別名 取消別名
[root@arslinux-01 ~]# unalias renet [root@arslinux-01 ~]# renet -bash: renet: 未找到命令 [root@arslinux-01 ~]# alias 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 rm='rm -i' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
8.4 通配符
* 任意個數的字符(包括 0 個)
? 任意一個字符
[0-9] 數字的範圍(括號中的字符只去一個)
[123] 括號中的幾個數字(任選其一,或的關係)
[a-z] 字母範圍
[A-Z] 類似上
[0-9a-zA-Z] 任意一個數字或字母
{1,2,3,a} 具體字符
https://blog.csdn.net/karelcn/article/details/83052395
[root@arslinux-01 ~]# ls *.txt 11111.txt 123.txt 1_hard.txt 1_soft.txt 1.txt 222.txt [root@arslinux-01 ~]# ls *txt* 11111.txt 123.txt 1_hard.txt 1_soft.txt 1.txt 2222.txt.bak 222.txt [root@arslinux-01 ~]# ls 1* 11111.txt 123.txt 1_hard.txt 1_soft.txt 1.txt 1.txz~ 111: 11.txt 222 ars3 123: yum.log
[root@arslinux-01 ~]# touch 2.txt [root@arslinux-01 ~]# touch 3.txt [root@arslinux-01 ~]# touch a.txt [root@arslinux-01 ~]# touch bb.txt [root@arslinux-01 ~]# ls ?.txt 1.txt 2.txt 3.txt a.txt
[root@arslinux-01 ~]# ls [0-2].txt 1.txt 2.txt [root@arslinux-01 ~]# ls [13].txt 1.txt 3.txt
[root@arslinux-01 ~]# ls [a-f].txt a.txt A.txt b.txt B.txt c.txt C.txt d.txt D.txt e.txt E.txt f.txt [root@arslinux-01 ~]# ls [A-F].txt A.txt b.txt B.txt c.txt C.txt d.txt D.txt e.txt E.txt f.txt F.txt
[root@arslinux-01 ~]# ls [0-9a-bA-D].txt 1.txt 2.txt 3.txt a.txt A.txt b.txt B.txt c.txt C.txt d.txt D.txt
[root@arslinux-01 ~]# ls {1,2,a,c}.txt 1.txt 2.txt a.txt c.txt
系統環境字母順序是 aAbBcCdDeEfF...xXyYzZ
8.5 輸入輸出重定向
輸出重定向:
> 前面正確的信息重定向到文本文件中
>> 前面正確的信息追加重定向到文本文件中
2> 錯誤重定向到文本文件中
2>> 錯誤追加重定向到文本文件中
&> 正確錯誤都重定向到文本文件中 1>+2>
&>> 正確錯誤都追加重定向到文本文件中
> a.txt 2>b.txt 正確的輸出到a.txt,錯誤的輸出到b.txt
command >1.txt 2>&1 正確的輸出到1.txt,錯誤的輸出到通道1,也就是1.txt
< 將右邊文本中的信息輸入重定向到前面的命令
https://www.cnblogs.com/divent/p/5773861.html
> 前面正確的信息重定向到文本文件中
[root@arslinux-01 ~]# ls 234/ ars ars1 arslinux arslinux1 [root@arslinux-01 ~]# ls 234/ > a.txt [root@arslinux-01 ~]# cat a.txt ars ars1 arslinux arslinux1
>> 前面正確的信息追加重定向到文本文件中
[root@arslinux-01 ~]# ls 234/ >> a.txt [root@arslinux-01 ~]# cat a.txt ars ars1 arslinux arslinux1 ars ars1 arslinux arslinux1
2> 錯誤重定向到文本文件中
[root@arslinux-01 ~]# lsss 234/ 2> a.txt [root@arslinux-01 ~]# cat a.txt -bash: lsss: 未找到命令
2>> 錯誤追加重定向到文本文件中
[root@arslinux-01 ~]# lsss 234/ 2>> a.txt [root@arslinux-01 ~]# cat a.txt -bash: lsss: 未找到命令 -bash: lsss: 未找到命令
&> 正確錯誤都重定向到文本文件中 1>+2>
[root@arslinux-01 ~]# ls 234/ 323/ &> a.txt [root@arslinux-01 ~]# cat a.txt ls: 無法訪問323/: 沒有那個文件或目錄 234/: ars ars1 arslinux arslinux1
&>> 正確錯誤都追加重定向到文本文件中
[root@arslinux-01 ~]# ls 234/ 323/ &>> a.txt [root@arslinux-01 ~]# cat a.txt ls: 無法訪問323/: 沒有那個文件或目錄 234/: ars ars1 arslinux arslinux1 ls: 無法訪問323/: 沒有那個文件或目錄 234/: ars ars1 arslinux arslinux1
> a.txt 2>b.txt 正確的輸出到a.txt,錯誤的輸出到b.txt
[root@arslinux-01 ~]# ls 234/ 323/ >a.txt 2> b.txt [root@arslinux-01 ~]# cat a.txt 234/: ars ars1 arslinux arslinux1 [root@arslinux-01 ~]# cat b.txt ls: 無法訪問323/: 沒有那個文件或目錄
< 將右邊文本中的信息輸入重定向到前面的命令
[root@arslinux-01 ~]# wc -l a.txt 5 a.txt [root@arslinux-01 ~]# wc -l < a.txt 5
同上
[root@arslinux-01 ~]# cat a.txt 234/: ars ars1 arslinux arslinux1 [root@arslinux-01 ~]# cat > newfile < a.txt [root@arslinux-01 ~]# cat newfile 234/: ars ars1 arslinux arslinux1
這裏的先將文件中的數據提取到了命令 cat 中 ,然後由 cat 寫入到 newfile 中
8.6 管道符和作業控制
管道:將前面命令輸出的結果交給後面的命令,用 | 隔開
[root@arslinux-01 ~]# cat a.txt 234/: ars ars1 arslinux arslinux1 [root@arslinux-01 ~]# cat a.txt | wc -l 5 [root@arslinux-01 ~]# cat a.txt | grep x arslinux arslinux1 [root@arslinux-01 ~]# ls | wc -l 27
ctrl z 暫停一個任務
jobs 查看後臺的任務
bg [id] 把任務調到後臺 (不加 id 就是最近一次)
fg [id] 把任務調到前臺
命令後面加& 直接丟到後臺
[root@arslinux-01 ~]# vim 1.txt
[1]+ 已停止 vim 1.txt [root@arslinux-01 ~]# df -h 文件系統 容量 已用 可用 已用% 掛載點 /dev/sda3 28G 2.1G 26G 8% / devtmpfs 476M 0 476M 0% /dev tmpfs 487M 0 487M 0% /dev/shm tmpfs 487M 7.7M 479M 2% /run tmpfs 487M 0 487M 0% /sys/fs/cgroup /dev/sda1 197M 105M 93M 54% /boot tmpfs 98M 0 98M 0% /run/user/0 [root@arslinux-01 ~]# fg vim 1.txt
[1]+ 已停止 vim 1.txt [root@arslinux-01 ~]# vim 2.txt
[2]+ 已停止 vim 2.txt [root@arslinux-01 ~]# jobs [1]- 已停止 vim 1.txt [2]+ 已停止 vim 2.txt [root@arslinux-01 ~]# fg vim 2.txt
[2]+ 已停止 vim 2.txt [root@arslinux-01 ~]# fg 1 vim 1.txt
[1]+ 已停止 vim 1.txt [root@arslinux-01 ~]# fg 2 vim 2.txt
[2]+ 已停止 vim 2.txt
bg 就不演示了
命令後面加& 直接丟到後臺
[root@arslinux-01 ~]# sleep 100 & [3] 7568 [root@arslinux-01 ~]# jobs [1]- 已停止 vim 1.txt [2]+ 已停止 vmstat 1 [3] 運行中 sleep 100 &
8.7/8.8 shell變量
PATH環境變量,HOME,PWD,LOGNAME等變量
env 查看系統的變量
[root@arslinux-01 ~]# env XDG_SESSION_ID=1 HOSTNAME=arslinux-01 SELINUX_ROLE_REQUESTED= TERM=xterm SHELL=/bin/bash HISTSIZE=5000 SSH_CLIENT=192.168.194.1 9072 22 SELINUX_USE_CURRENT_RANGE= SSH_TTY=/dev/pts/0 USER=root LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36: MAIL=/var/spool/mail/root PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin PWD=/rootLANG=zh_CN.UTF-8 SELINUX_LEVEL_REQUESTED= HISTCONTROL=ignoredups SHLVL=1 HOME=/root LOGNAME=root SSH_CONNECTION=192.168.194.1 9072 192.168.194.130 22 LESSOPEN=||/usr/bin/lesspipe.sh %s XDG_RUNTIME_DIR=/run/user/0 _=/usr/bin/env
set 查看所有變量(不僅查看系統內置的環境變量,也會有用戶自定義的變量)
·自定義變量
[root@arslinux-01 ~]# a=111 [root@arslinux-01 ~]# echo $a 111 [root@arslinux-01 ~]# set |grep 111 _=111 a=111
·變量名規則:字母、數字下劃線,首位不能爲數字
a1 √
a_1 √
_a1 √
1a ×
·變量值有特殊符號時需要用單引號括起來
[root@arslinux-01 ~]# a='a b c' [root@arslinux-01 ~]# echo $a a b c [root@arslinux-01 ~]# a=a b c -bash: b: 未找到命令 [root@arslinux-01 ~]# echo $a a b c
單引號有脫義的作用
[root@arslinux-01 ~]# a="a$bc" [root@arslinux-01 ~]# echo $a a [root@arslinux-01 ~]# a='a$bc' [root@arslinux-01 ~]# echo $a a$bc
變量的累加
[root@arslinux-01 ~]# a=1 [root@arslinux-01 ~]# b=2 [root@arslinux-01 ~]# echo $a$b 12 [root@arslinux-01 ~]# a='a$bc' [root@arslinux-01 ~]# echo $a$b a$bc2 [root@arslinux-01 ~]# c="a$bc" [root@arslinux-01 ~]# echo $c a [root@arslinux-01 ~]# c="a$b"c [root@arslinux-01 ~]# echo $c a2c [root@arslinux-01 ~]# c=a"$b"c [root@arslinux-01 ~]# echo $c a2c
本地變量
查看自己在哪一個登錄的終端
[root@arslinux-01 ~]# w 11:15:07 up 1:18, 2 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.194.1 09:57 3.00s 0.14s 0.03s w root pts/1 192.168.194.1 11:15 2.00s 0.04s 0.04s -bash [root@arslinux-01 ~]# echo $SSH_TTY /dev/pts/0
定義一個變量,在子bash中,自定義的變量沒有生效
[root@arslinux-01 ~]# arslinux=ars111 [root@arslinux-01 ~]# echo $arslinux ars111 [root@arslinux-01 ~]# bash [root@arslinux-01 ~]# pstree systemd─┬─NetworkManager───2*[{NetworkManager}] ├─VGAuthService ├─agetty ├─auditd───{auditd} ├─chronyd ├─crond ├─dbus-daemon───{dbus-daemon} ├─firewalld───{firewalld} ├─lvmetad ├─master─┬─pickup │ └─qmgr ├─polkitd───6*[{polkitd}] ├─rsyslogd───2*[{rsyslogd}] ├─sshd─┬─sshd───bash───bash───pstree##當前位置 │ └─sshd───bash ├─systemd-journal ├─systemd-logind ├─systemd-udevd ├─tuned───4*[{tuned}] └─vmtoolsd───{vmtoolsd} [root@arslinux-01 ~]# echo $arslinux [root@arslinux-01 ~]#
退回到原bash,自定義變量又生效
[
root@arslinux-01 ~]# exit exit [root@arslinux-01 ~]# pstree systemd─┬─NetworkManager───2*[{NetworkManager}] ├─VGAuthService ├─agetty ├─auditd───{auditd} ├─chronyd ├─crond ├─dbus-daemon───{dbus-daemon} ├─firewalld───{firewalld} ├─lvmetad ├─master─┬─pickup │ └─qmgr ├─polkitd───6*[{polkitd}] ├─rsyslogd───2*[{rsyslogd}] ├─sshd─┬─sshd───bash───pstree##目前位置 │ └─sshd───bash ├─systemd-journal ├─systemd-logind ├─systemd-udevd ├─tuned───4*[{tuned}] └─vmtoolsd───{vmtoolsd} [root@arslinux-01 ~]# echo $arslinux ars111
這種僅僅在當前終端下生效的變量叫本地變量
全局變量
export 變量=值 定義全局變量(全局變量只在它和它的子shell中生效)
[root@arslinux-01 ~]# export arslinux=ars1101 [root@arslinux-01 ~]# echo $arslinux ars1101 [root@arslinux-01 ~]# bash [root@arslinux-01 ~]# echo $arslinux ars1101 [root@arslinux-01 ~]# bash [root@arslinux-01 ~]# echo $arslinux ars1101
定義的全局在其他終端下不生效
在子shell中定義的全局變量,無法在上一層shell中生效
[root@arslinux-01 ~]# export b=ars12345 [root@arslinux-01 ~]# echo $b ars12345 [root@arslinux-01 ~]# exit exit [root@arslinux-01 ~]# echo $b [root@arslinux-01 ~]#
unset 變量名 取消變量
[root@arslinux-01 ~]# echo $arslinux ars1101 [root@arslinux-01 ~]# unset arslinux [root@arslinux-01 ~]# echo $arslinux [root@arslinux-01 ~]#
8.9 環境變量配置文件
系統的環境變量配置文件有兩個維度:系統層次、用戶層次
系統層次:/etc/profile 用戶環境變量,交互,登錄才執行
/etc/bashrc 用戶不用登錄,執行shell就生效
profile和bashrc的區別是,profile是用戶登錄時加載執行的,而bashrc是執行shell腳本時生效的
(以上兩個目錄請勿亂動,平時編輯用戶家目錄下的即可,系統層次的可以全局生效,而用戶層次只針對該用戶生效)
配置文件被修改後,可以用source 文件或者 . 文件來重新加載
例如:source /etc/profile 或 . /etc/profile
用戶層次:~/.bashrc 用戶命令別名等個性化
~/.bash_profile 爲用戶設置環境信息
~/.bash_history 存放用戶命令歷史
~/.bash_logout 用來定義用戶退出時需要做的一些操作
如果想讓用戶每次退出時都刪除命令歷史,那麼可以用把刪除命令歷史的命令放到 .bash_logout 中去
變量PS1定義提示符,在 /etc/bashrc 下修改
[root@arslinux-01 ~]# PS1='[\u@\h \w]\$' [root@arslinux-01 ~]#echo $PS1 [\u@\h \w]\$ [root@arslinux-01 ~]#cd /etc/sysconfig/network-scripts/ [root@arslinux-01 /etc/sysconfig/network-scripts]#PS1='\u@\h \w\$' root@arslinux-01 /etc/sysconfig/network-scripts#
將 $PS1 中的 W 改爲 w,路徑變爲了絕對路徑,去掉方括號的話,同樣提示符方括號也會去掉
$PS1 的 [\u@\h \w]\$ 的 $ ,普通用戶是 $ ,root 用戶是 #
可以設置帶顏色顯示
PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ '
PS2 是用在另一種環境裏的,例如mysql中會使用到
[root@arslinux-01 ~]#echo $PS2 > [root@arslinux-01 ~]#for i in `seq 1 10` > do > echo $i > done 1 2 3 4 5 6 7 8 9 10
擴展
bashrc和bash_profile的區別 http://ask.apelearn.com/question/7719
0402課堂筆記
bashrc和bash_profile
http://ask.apelearn.com/question/7719
http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html
==> /etc/profile
==> ~/.bash_profile
~/.bash_login
~/.profile
==> ~/.bashrc
==> /etc/bashrc
==> ~/.bash_logout
關於PROMPT_COMMAND環境變量的含義
http://www.linuxnote.org/prompt_command-environment-variables.html
PROMPT_COMMAND='ECHO -NE "\033[31mAming\033[0m\033[33m teach\033[0m\033[32m Linux\033[0m"'
echo 顏色:http://www.cnblogs.com/lr-ting/archive/2013/02/28/2936792.html
source和exec的區別
http://alsww.blog.51cto.com/2001924/1113112
cat 2.sh #!/bin/bash echo "children script PID is $$" echo "now to children script." export a=2 echo "in children, a=$a"
cat source.sh #!/bin/bash echo "fater script PID is $$" echo "fater script, set a is 1" export a=1 echo "it will source ./2.sh" source ./2.sh echo "now to father script." echo "fater script, a is $a"
cat exec.sh #!/bin/bash echo "fater script PID is $$" echo "fater script, set a is 1" export a=1 echo "it will exec ./2.sh" exec ./2.sh echo "fater script, a is $a"
exec:執行子級的命令後,不再執行父級命令
source:執行子級命令後繼續執行父級命令
找具體哪個包安裝:yum provides "/*/*bin/vim"