1. 程式人生 > >linux shell之bash的特性

linux shell之bash的特性

1. 命令別名

  • 檢視當前使用者別名定義:alias

示例:
[[email protected] ~]# 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’

  • 定義別名:alias NAME=’COMMAND’

示例:
[[email protected] mnt]# alias ls=’ls -l’
[[email protected] mnt]# ls
總用量 828
dr-xr-xr-x. 4 root root 2048 2月 20 2015 addons
dr-xr-xr-x. 3 root root 2048 2月 20 2015 EFI
-r–r–r–. 1 root root 8266 4月 4 2014 EULA
-r–r–r–. 1 root root 18092 3月 6 2012 GPL
dr-xr-xr-x. 3 root root 2048 2月 20 2015 images
dr-xr-xr-x. 2 root root 2048 2月 20 2015 isolinux
dr-xr-xr-x. 2 root root 2048 2月 20 2015 LiveOS
-r–r–r–. 1 root root 114 2月 19 2015 media.repo
dr-xr-xr-x. 2 root root 790528 2月 20 2015 Packages

  • 撤銷別名:unalias NAME

示例:
[[email protected] mnt]# unalias ls
[[email protected] mnt]# ls
addons images Packages RPM-GPG-KEY-redhat-release
EFI isolinux release-notes TRANS.TBL
EULA LiveOS repodata
GPL media.repo RPM-GPG-KEY-redhat-beta

2.命令歷史

  • 檢視命令歷史列表:history(檔案預設存放在~/.bash_history)

示例:
[[email protected] mnt]# history
1 alias ls=’ls -l’
2 ls
3 unalias ls
4 ls
5 history

  • 呼叫歷史列表中的命令:

    !#:再一次執行歷史列表中的第#條命令;

    !!:再一次執行上一條命令,也可以用向上箭頭;

    !STRING:再一次執行命令歷史列表最近一個以STRING開頭的命令;

示例:
[[email protected] mnt]# !162
cd /home/
[[email protected] home]# !!
cd /home/
[[email protected] home]# !l
ls
a b c guox ln02 ln03 test

  • 其他常用引數:
    -c:清空命令歷史;

    -d offset:刪除指定命令歷史;

    -r : 從檔案讀取命令歷史追加至歷史檔案中;

    -w:把歷史列表中的命令追加至歷史檔案中;

    histroy #:顯示最近的#條命令;

3.快捷鍵

Ctrl+a:跳轉至命令列行首
Ctrl+e:跳轉至命令列行尾
Ctrl+u:刪除行首至游標所在處之間的所有字元;
Ctrl+k:刪除游標所在處至行尾的所有字元;
Ctrl+l:清屏,相當於clear

4.命令補全和路徑補全

  • 命令查詢和補全機制:
    (1) 查詢內部命令;
    (2) 根據PATH環境變數中設定的目錄,自左而右逐個搜尋目錄下的檔名;
    (3) 給定開頭的部分字串如果能惟一標識某命令程式檔案,則直接補全;
    不能惟一標識某命令程式檔案,再按tab鍵一次,會給出列表;

  • 路徑補全:

    在給定的起始路徑的上級目錄下,以對應路徑下的打頭字串來逐一匹配上級目標下的每個檔案:

    (1) 惟一標識:tab補全;

    (2) 不能惟一標識:兩次tab給出列表;

    (3) 錯誤路徑:沒有響應;

示例:
[[email protected] ~]# user
useradd userformat userinfo usermount userpasswd
userdel userhelper usermod usernetctl users
[[email protected] ~]# cd /home/
a/ b/ c/ guox/

5.命令列展開

把命令列的給定的特殊符號自動替換為相應字串的機制;

~: 自動替換為使用者家目錄;

~USERNAME:自動替換為指定使用者的家目錄;

{}:可承載一個以逗號分隔的路徑列表,能夠將其展開為多個獨立路徑;

示例:
[[email protected] home]# cd ~
[[email protected] ~]# pwd
/root
[[email protected] ~]# cd ~guox
[[email protected] guox]# pwd
/home/guox
[[email protected] guox]# mkdir -p {a,b,c}
[[email protected] guox]# ll
總用量 0
drwxr-xr-x. 2 root root 6 9月 25 10:16 a
drwxr-xr-x. 2 root root 6 9月 25 10:16 b
drwxr-xr-x. 2 root root 6 9月 25 10:16 c

6.命令的執行狀態結果

  • 命令的執行狀態結果:

    成功:0
    失敗:1-255

一個特殊變數來儲存最一次執行的命令的狀態結果:$?

示例:
[[email protected] guox]# echo $HOSTNAME
guox.top
[[email protected] guox]# echo $?
0
[[email protected] guox]# echoabc $HOSTNAME
bash: echoabc: 未找到命令…
[[email protected] guox]# echo $?
127

7.glob

  • 檔名通配;快速引用多個檔案;檔名整體匹配度檢測;
  • 元字元:基於元字元可編寫匹配模式(pattern):
元字元 作用
* 匹配任意長度的任意字元
? 匹配任意單個字元
[ ] 匹配指定集合內的任意單個字元
[^ ] 匹配指定集合外的任意單個字元
^ 匹配一行的開頭位置
$ 匹配一行的結束位置
^$ 匹配空行

示例:
[[email protected] bin]# ls w?
wc
[[email protected] bin]# ls y*
yelp ypdomainname yum-builddep yum-debug-dump yumdownloader
yes yum yum-config-manager yum-debug-restore yum-groups-manager