shell中alias命令的使用
0.什么是alias
- 設(shè)置指令的別名,用戶可利用alias,自定指令的別名。
- 若僅輸入alias,則可列出目前所有的別名設(shè)置。
- alias的效力僅及于該次登入的操作。若要每次登入是即自動設(shè)好別名,可在/etc/profile或自己的~/.bashrc中設(shè)定指令的別名。
- 如果要給每一位用戶都生效的別名,請把alias la=‘ls -al’ 一行加在/etc/bashrc最后面。
- bashrc是環(huán)境變量的配置文件 /etc/bashrc和~/.bashrc 區(qū)別就在于一個是設(shè)置給全系統(tǒng)一個是設(shè)置給單用戶使用
1.Shell alias
Shell alias:給命令創(chuàng)建別名
[root@foundation0 ~]# alias 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' #你看,為了讓我們使用方便,Shell 會給某些命令默認創(chuàng)建別名
alias new_name='command'
比如,一般的關(guān)機命令是shutdown-h now,寫起來比較長,這時可以重新定義一個關(guān)機命令,以后就方便多了。
alias myShutdown=‘shutdown -h now'
再如,通過 date 命令可以獲得當(dāng)前的 UNIX 時間戳,具體寫法為date +%s,如果你嫌棄它太長或者不容易記住,那可以給它定義一個別名。
alias timestamp=‘date +%s' [root@server1 mnt]# sh test.sh? run time: 20s [root@server1 mnt]# cat test.sh? #!/bin/bash alias timestamp='date +%s' begin=`timestamp` ? sleep 20s finish=$(timestamp) difference=$((finish - begin)) echo "run time: ${difference}s"
別名只是臨時的
在代碼中使用 alias 命令定義的別名只能在當(dāng)前 Shell 進程中使用,在子進程和其它進程中都不能使用。當(dāng)前 Shell 進程結(jié)束后,別名也隨之消失。
要想讓別名對所有的 Shell 進程都有效,就得把別名寫入 Shell 配置文件。Shell 進程每次啟動時都會執(zhí)行配置文件中的代碼做一些初始化工作,將別名放在配置文件中,那么每次啟動進程都會定義這個別名
2.使用 unalias 命令刪除別名
使用 unalias 內(nèi)建命令可以刪除當(dāng)前 Shell 進程中的別名。unalias 有兩種使用方法:
- 第一種用法是在命令后跟上某個命令的別名,用于刪除指定的別名。
- 第二種用法是在命令后接-a參數(shù),刪除當(dāng)前 Shell 進程中所有的別名
同樣,這兩種方法都是在當(dāng)前 Shell 進程中生效的。要想永久刪除配置文件中定義的別名,只能進入該文件手動刪除
到此這篇關(guān)于shell中alias命令的使用的文章就介紹到這了,更多相關(guān)shell alias內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用shell檢查并修復(fù)mysql數(shù)據(jù)庫表的腳本
這篇文章主要介紹了使用shell檢查并修復(fù)mysql數(shù)據(jù)庫表的腳本,需要的朋友可以參考下2014-03-03