1. 程式人生 > >夯實基礎 ① Linux 內部與外部命令

夯實基礎 ① Linux 內部與外部命令

linux中的命令大致可分為兩類,內部命令和外部命令:

內部命令(builtin command):也稱shell內嵌命令

外部命令(external command):存放在一個檔案中,使用時需要去檔案中查詢,這些檔案被定義在$PATH

。。。說道內部和外部命令就不得不提及到type命令,一個專門用來顯示指定命令的型別的命令,其本身也是內部命令。

type命令用來顯示指定命令的型別,判斷給出的指令是內部指令還是外部指令。

  • alias:別名。
  • keyword:關鍵字,Shell保留字。
  • function:函式,Shell函式。
  • builtin:內建命令,Shell內建命令。
  • file:檔案,磁碟檔案,外部命令。
  • unfound:沒有找到。
語法:type(選項)(引數)
選項:
-t:輸出“file”、“alias”或者“builtin”,分別表示給定的指令為“外部指令”、“命令別名”或者“內部指令”; -p:如果給出的指令為外部指令,則顯示其絕對路徑; -a:在環境變數“PATH”指定的路徑中,顯示給定指令的資訊,包括命令別名。
引數:指令:要顯示型別的指令。

內部命令使用者輸入時系統呼叫的速率快,不是內部命令,系統將會讀取環境變數檔案/etc/profile去找PATH路徑。

然後在提一下命令的呼叫,有些歷史命令使用過後,會存在在hash表中,當你再次輸入該命令它的呼叫會是這樣一個過程。

hash——>內建命令——>PATH   命令的呼叫其實應該是這樣一個過程。

[[email protected] ~]# type pwd
pwd is a shell builtin
[[email protected] ~]# type cat cat is /usr/bin/cat [[email protected] ~]# pwd /root [[email protected] ~]# ls linuxeye* linuxeye.pem linuxeye.txt [[email protected] ~]# cat linuxeye.txt linuxeye [
[email protected]
~]# hash -l #顯示hash表 builtin hash -p /usr/bin/cat cat builtin hash -p /usr/bin/ls ls [[email protected] ~]# type cat cat is hashed (/usr/bin/cat) [[email protected] ~]# hash -r #清除hash表
從上面的小實驗可以看出。hash表不存放系統內建命令。

命令選自http://man.linuxde.net/
實驗選自https://www.cnblogs.com/Awhipltz/articles/4823506.html