Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Linux 內部命令與外部命令解析:type、enable 與 hash 一次搞懂

Linux 強大的命令令人印象深刻,但許多用戶對其背後機制並不熟。本文帶你看懂內部命令與外部命令的差別,以及查詢與快取機制。

Linux 內部命令與外部命令解析:type、enable 與 hash 一次搞懂

內部命令與外部命令

內部命令在系統啟動時就載入記憶體、常駐其中,執行效率高;外部命令則是額外的軟體功能,需要時才從硬碟讀入。

用 type 可判斷是否為內建命令:

[root@linuxeye ~]# type type
type is a shell builtin
[root@linuxeye ~]# type -t type
builtin
[root@linuxeye ~]# type whiptail
whiptail is /usr/bin/whiptail

enable 既能列出內部命令,也能判斷某命令是否為內建:

[root@linuxeye ~]# enable -a 列出全部
[root@linuxeye ~]# enable whiptail 顯示 not a shell builtin
[root@linuxeye ~]# enable pwd 是內建命令

非內建命令時,系統會讀取環境變數檔(.bash_profile、/etc/profile)中的 PATH 去搜尋。

命令的快取(hash)機制

用過的外部命令會存入 hash 表,再次呼叫時的順序為:hash → 內建命令 → PATH。例如:

[root@linuxeye ~]# hash -l 顯示 hash 表
builtin hash -p /usr/bin/cat cat
[root@linuxeye ~]# type cat
cat is hashed (/usr/bin/cat)
[root@linuxeye ~]# hash -r 清除 hash 表
[root@linuxeye ~]# type cat
cat is /usr/bin/cat

由此可見,hash 表不存放系統內建命令。理解這些機制,就能更精準地使用 Linux 命令。

Copyright © Linux教程網 All Rights Reserved