Linux啟動後,右鍵打開terminal,就可以輸入指令了
Which 可以檢視外部指令的路徑
執行個體: [root@centos7 ~]# which who
/usr/bin/who
[root@centos7 ~]#
和which相似的指令還有wheris,不僅可以顯示指令路徑,還可以顯示出指令相關的文檔路徑
執行個體: [root@centos7 ~]# whereis who
who: /usr/bin/who /usr/share/man/man1/who.1.gz /usr/share/man/man1p/who.1p.gz
[root@centos7 ~]#
Which有些時候還可以顯示出,指令的别名。
執行個體:[root@centos7 ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@centos7 ~]#
Alias就是下面所要說的别名指令。
别名就是對一個比較長的指令寫一個短的别稱,以後運作時比較友善
例如我們經常要在cd /etc/sysconfig/network-scripts/這個目錄下操作網絡相關的資訊,我們就可以把這個指令定義别名cdnet,執行指令的效果的是一模一樣。
alias+别名,可以顯示定義成功後的别名資訊
執行個體:[root@centos7 ~]# alias cdnet="cd /etc/sysconfig/network-scripts/"
[root@centos7 ~]# alias cdnet
alias cdnet='cd /etc/sysconfig/network-scripts/'
[root@centos7 ~]# cdnet
[root@centos7 network-scripts]# cd ~
[root@centos7 ~]# cd /etc/sysconfig/network-scripts/
[root@centos7 network-scripts]#
注意 =号前後不能有空格
系統中本來就有一些别名指令,可以通過alias這個指令檢視這些别名。
執行個體:[root@centos7 ~]# alias
alias cdnet='cd /etc/sysconfig/network-scripts/'
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'
[root@centos7 ~]#
當目前使用者退出後,重新登入系統,前面剛定義的cdnet就消失了,不起作用,在用Alisa指令檢視就找不到了。若要不消失就要儲存在目前使用者的家目錄的.bashrc檔案中,如下圖
打開檔案次檔案,把alias cdnet='cd /etc/sysconfig/network-scripts/'此指令儲存在檔案中,然後加載配置資訊,就可以永久運作了。
執行個體[root@centos7 ~]# nano .bashrc
這樣儲存後不會立即生效。
簡單的方法退出,重新登入後就可以生效。或者用. .bahrc和source .bashrc 執行後也可以生效。
這個.本來就是一個内部指令。
執行個體 [root@centos7 ~]# source .bashrc
[root@centos7 ~]# cdnet
[root@centos7 network-scripts]#
Root使用者定義的别名對其他使用者無效,若要全局有效就要增加到/etc/bashrc中,一般不建議。
有一些特殊的指令别名和原來的指令是同一個名稱,執行順序就要注意了,,優先級 别名》内部指令>外部指令,
比如ls指令就是特殊的,既是别名也是一個外部指令,執行時肯定先執行别名,若要執行原始的外部指令就得有特殊的轉義。
執行個體
Which指令中可以看到别名,和外部指令
Ls執行别名時帶顔色,原始指令不帶顔色,執行原始指令的方法有
1輸入原始指令路徑 /bin/ls (隻适合外部指令)
2 單引号 ‘ls’
3 反斜杠 \ls
4 雙引号
5 comand ls comand 指令也可以
執行個體
取消别名 用unalias
Unalias -a 取消所有的别名