1. 命令的概念
-
命令的執行過程
系統第一次執行外部命令時Hash緩存表為空,系統會先從PTAH路徑下尋找命令,找到后會將路徑加入到Hasa緩存中,當再次執行此命令時會直接從Hash的路徑下執行,如果存在直接執行,如果不存在將繼續從PATH下的路徑繼續查找,Hash表可以提高命令的調用速率。
-
命令的優先級
alias ————————————-別名
??builtin——————————內部命令
????hash————————-緩存表
??????$PATH—————可執行程序或腳本(外部命令) -
內部命令與外部命令
內部命令是shell自帶的
外部命令是安裝系統時默認安裝的,并且在文件系統下有對應的路徑 -
查看命令是內部命令還是外部命令type [commnd]
[root@centos6 ~]# type cat #判斷cat命令,外部命令顯示文件路徑 cat is /bin/cat [root@centos6 ~]# type cd #判斷cd命令 cd is a shell builtin
2.命令的別名
命名別名只在當前進程中有效
如果想永久有效,要定義在配置文件中
??僅對當前用戶:~/.bashrc
??對所有用戶有效:/etc/bashrc -
查看進程中所有的別名 alias
[root@centos6 ~]#alias alias cp='cp -i' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' ......
-
定義別名 alias NAME=”VALUE”
[root@centos6 ~]#alias aubin=cat [root@centos6 ~]#aubin test hello world
-
刪除別名
[root@centos6 ~]#unalias aubin [root@centos6 ~]#aubin test -bash: aubin: command not found
-
定義對當前用戶永久生效的別名
[root@centos6 ~]#vim .bashrc # .bashrc # User specific aliases and functions alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' alias aubin=cat # <<<-----此處定義別名 # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi [root@centos6 ~]#. .bash #立即生效
-
定義指定用戶生效的別名
[root@centos6 ~]#cd ~ li [root@centos6 li]#vim .bashrc #編輯用戶目錄下的.bashrc
-
定義所有用戶生效的別名
[root@centos6 ~]#vim /etc/bashrc alias aubin=cat # <<<-----加入定義別名 [root@centos6 ~]#. /etc/bashrc #立即生效
3.內部命令
shell程序找到鍵入命令所對應的可執行程序或代碼,由shell分析后提交給內核分配資源并將其運行起來。
-
查看所有的內部命令
[root@centos6 ~]#help
[root@centos6 ~]#enable enable . enable : enable [ enable alias enable bg enable bind ......
-
內部命令的禁用與啟用enable
[root@centos6 li]#enable -n cd #禁用內部命令 [root@centos6 li]#enable cd #啟用內部命令
-
禁用內部命令失效
[root@centos6 li]#enable -n pwd [root@centos6 li]#enable -n #查看禁用的內部命令或如下圖用help enable -n pwd
也可以help查已經被禁用的命令【命令前的*代表命令已經用】
禁用內部命令enable -n pwd后依然可以使用[root@centos6 li]#pwd /home/li
使用which查看命令的執行文件
[root@centos6 li]#which pwd /bin/pwd
當內部命令禁用后,按照bash優先級繼續搜索Hash表、(PATH。直到在)PATH中發現/bin/pwd的可執行文件則將其運行。
-
查看禁用的內部命令
[root@centos6 li]#enable -n enable -n cd enable -n pwd
或者如上圖所示使用help命令查看
4.HASH緩存表
用來顯示和清除哈希表,執行命令的時候,系統將先查詢哈希表。
-
查看命令的緩存 hash
[root@centos6 ~]# hash hits command 3 /usr/bin/cal 1 /usr/bin/yum
[root@centos6 ~]# 查看詳細的Hash表 [root@centos6 ~]#hash -l builtin hash -p /bin/dd dd builtin hash -p /usr/bin/yum yum
-
向Hash表中增加內容 hash -p path command
[root@centos6 ~]#將cat定義一個別名存在hash表 [root@centos6 ~]#hash -p /bin/cat aubin [root@centos6 ~]#aubin test hello world
-
打印Hash表中命令的路徑 hash -t command
[root@centos6 ~]#hash -t aubin /bin/cat
-
刪除Hash表中指定命令 hash -d command
[root@centos6 ~]#hash -d aubin
-
刪除Hash表中所有命令hash -r
[root@centos6 ~]# hash -r
-
查看命令的路徑 which
[root@centos6 ~]# which cat #查看命令的路徑,以第一個路徑為準 /bin/cat [root@centos6 ~]# which -a cat #查看命令所有路徑,一個命令可能有多個路徑 /bin/cat /usr/local/bin/cat
5.外部命令
外部命令就是一個可執行文件,當執行外部命令時,系統會去執行在文件目錄下對應的可執行文件。
-
列出命令的路徑
[root@centos6 /]#which echo #列出命令的路徑 /bin/echo
[root@centos6 /]#which cp #which列出文件路徑會顯示別名 alias cp='cp -i' /bin/cp [root@centos6 /]#which --skip-alias cp #列出文件路徑而不顯示別名 /bin/cp
-
列出命令所有路徑,多個bash有相同命令時,則命令有多個路徑。
[root@centos6 /]#which -a echo /bin/echo
-
列出命令與幫助手冊的路徑
[root@centos6 /]#whereis echo echo: /bin/echo /usr/share/man/man1/echo.1.gz /usr/share/man/man1p/echo.1p.gz