閱讀630 返回首頁    go 阿裏雲 go 技術社區[雲棲]


理解 Linux 下 Shell 命令的不同分類及它們的用法

當你打算真正操縱好你的 Linux 係統,沒有什麼能比命令行界麵更讓你做到這一點。為了成為一個 Linux 高手,你必須能夠理解 Shell 命令的不同類型,並且會在終端下正確的使用它們。

在 Linux 下,命令有幾種類型,對於一個 Linux 新手來說,知道不同命令的意思才能夠高效和準確的使用它們。因此,在這篇文章裏,我們將會遍及各種不同分類的 Linux Shell 命令。

需要注意一件非常重要的事:命令行界麵和 Shell 是不同的,命令行界麵隻是為你提供一個訪問 Shell 的方式。而 Shell ,它是可編程的,這使得它可以通過命令與內核進行交流。

下麵列出了 Linux 下命令的不同種類:

1. 程序可執行文件(文件係統 中的命令)

當你執行一條命令的時候,Linux 通過從左到右搜索存儲在 $PATH 環境變量中的目錄來找到這條命令的可執行文件。

你可以像下麵這樣查看存儲在 $PATH 中的目錄:


  1. $ echo $PATH
  2. /home/aaronkilik/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

在上麵的命令中,目錄 /home/aaronkilik/bin 將會被首先搜索,緊跟著是 /usr/local/sbin,然後一直接著下去。在搜索過程中,搜索順序是至關重要的。

比如在 /usr/bin 目錄裏的文件係統中的命令:


  1. $ ll /bin/

示例輸出:


  1. total 16284
  2. drwxr-xr-x 2 root root 4096 Jul 31 16:30 ./
  3. drwxr-xr-x 23 root root 4096 Jul 31 16:29 ../
  4. -rwxr-xr-x 1 root root 6456 Apr 14 18:53 archdetect*
  5. -rwxr-xr-x 1 root root 1037440 May 17 16:15 bash*
  6. -rwxr-xr-x 1 root root 520992 Jan 20 2016 btrfs*
  7. -rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfs-calc-size*
  8. lrwxrwxrwx 1 root root 5 Jul 31 16:19 btrfsck -> btrfs*
  9. -rwxr-xr-x 1 root root 278376 Jan 20 2016 btrfs-convert*
  10. -rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfs-debug-tree*
  11. -rwxr-xr-x 1 root root 245368 Jan 20 2016 btrfs-find-root*
  12. -rwxr-xr-x 1 root root 270136 Jan 20 2016 btrfs-image*
  13. -rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfs-map-logical*
  14. -rwxr-xr-x 1 root root 245368 Jan 20 2016 btrfs-select-super*
  15. -rwxr-xr-x 1 root root 253816 Jan 20 2016 btrfs-show-super*
  16. -rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfstune*
  17. -rwxr-xr-x 1 root root 245368 Jan 20 2016 btrfs-zero-log*
  18. -rwxr-xr-x 1 root root 31288 May 20 2015 bunzip2*
  19. -rwxr-xr-x 1 root root 1964536 Aug 19 2015 busybox*
  20. -rwxr-xr-x 1 root root 31288 May 20 2015 bzcat*
  21. lrwxrwxrwx 1 root root 6 Jul 31 16:19 bzcmp -> bzdiff*
  22. -rwxr-xr-x 1 root root 2140 May 20 2015 bzdiff*
  23. lrwxrwxrwx 1 root root 6 Jul 31 16:19 bzegrep -> bzgrep*
  24. -rwxr-xr-x 1 root root 4877 May 20 2015 bzexe*
  25. lrwxrwxrwx 1 root root 6 Jul 31 16:19 bzfgrep -> bzgrep*
  26. -rwxr-xr-x 1 root root 3642 May 20 2015 bzgrep*

2. Linux 別名

這些是用戶定義的命令,它們是通過 shell 內置命令 alias 創建的,其中包含其它一些帶有選項和參數的 shell 命令。這個意圖主要是使用新穎、簡短的名字來替代冗長的命令。

創建一個別名的語法像下麵這樣:


  1. $ alias newcommand='command -options'

通過下麵的命令,可以列舉係統中的所有別名:


  1. $ alias -p
  2. alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
  3. alias egrep='egrep --color=auto'
  4. alias fgrep='fgrep --color=auto'
  5. alias grep='grep --color=auto'
  6. alias l='ls -CF'
  7. alias la='ls -A'
  8. alias ll='ls -alF'
  9. alias ls='ls --color=auto'

要在 Linux 中創建一個新的別名,仔細閱讀下麵的例子。


  1. $ alias update='sudo apt update'
  2. $ alias upgrade='sudo apt dist-upgrade'
  3. $ alias -p | grep 'up'

然而,上麵這些我們創建的別名隻能暫時的工作,當經過下一次係統啟動後它們不再工作。你可以像下麵展示的這樣在 '.bashrc' 文件中設置永久別名。

添加以後,運行下麵的命令來激活:


  1. $ source ~/.bashrc

3. Linux Shell 保留字

在 shell 程序設計中,ifthenfiforwhilecaseesacelseuntil 以及其他更多的字都是 shell 保留字。正如描述所暗示的,它們在 shell 中有特殊的含義。

你可以通過使用下麵展示的 type 命令來列出所有的 shell 關鍵字:


  1. $ type if then fi for while case esac else until
  2. if is a shell keyword
  3. then is a shell keyword
  4. fi is a shell keyword
  5. for is a shell keyword
  6. while is a shell keyword
  7. case is a shell keyword
  8. esac is a shell keyword
  9. else is a shell keyword
  10. until is a shell keyword

4. Linux shell 函數

一個 shell 函數是一組在當前 shell 內一起執行的命令。函數有利於在 shell 腳本中實現特殊任務。在 shell 腳本中寫 shell 函數的傳統形式是下麵這樣:


  1. function_name() {
  2. command1
  3. command2
  4. ......
  5. }

或者像這樣:


  1. function function_name {
  2. command1
  3. command2
  4. ......
  5. }

讓我們看一看如何在一個名為 shell_functions.sh 的腳本中寫 shell 函數。


  1. #!/bin/bash
  2. #write a shell function to update and upgrade installed packages
  3. upgrade_system(){
  4. sudo apt update;
  5. sudo apt dist-upgrade;
  6. }
  7. #execute function
  8. upgrade_system

取代通過命令行執行兩條命令:sudo apt update 和 sudo apt dist-upgrade,我們在腳本內寫了一個像執行一條單一命令一樣來執行兩條命令的 shell 函數 upgrade_system。

保存文件,然後使腳本可執行。最後像下麵這樣運行 shell 函數:


  1. $ chmod +x shell_functions.sh
  2. $ ./shell_functions.sh

5. Linux Shell 內置命令

這些是在 shell 中內置的 Linux 命令,所以你無法在文件係統中找到它們。這些命令包括pwdcdbgaliashistorytypesourcereadexit 等。

你可以通過下麵展示的 type 命令來列出或檢查 Linux 內置命令:


  1. $ type pwd
  2. pwd is a shell builtin
  3. $ type cd
  4. cd is a shell builtin
  5. $ type bg
  6. bg is a shell builtin
  7. $ type alias
  8. alias is a shell builtin
  9. $ type history
  10. history is a shell builtin

學習一些 Linux 內置命令用法:

原文發布時間為:2016-09-25

本文來自雲棲社區合作夥伴“Linux中國”

最後更新:2017-06-06 16:32:13

  上一篇:go  Ohm:用兩百行 JavaScript 創造你自己的編程語言
  下一篇:go  20 分鍾構建你自己的 Linux 發行版