3.6-變量別名 命令別名

命令別名將爲公共命令及它們的參數創建別名,以儘可能減少錄入工作。

1、查看系統已存在的公共命令別名
使用 alias -p 查看系統已存在的公共命令別名 活動列表

[root@hadoop ~]# alias -p
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'

2、使用alias創建命令別名

[root@hadoop ~]# alias li='ls -li'
[root@hadoop ~]# li
總用量 112
33574979 -rw-------. 1 root root  1419 6月  11 13:33 anaconda-ks.cfg
51251036 -rw-r--r--. 1 root root 22255 7月   8 13:53 customer.java
19751199 -rw-r--r--. 1 root root 20640 7月   8 13:53 date_dim.java
34855665 -rw-r--r--. 1 root root     0 7月   8 20:38 derby.log
51251011 -rw-r--r--. 1 root root 14215 7月   7 14:55 product.java
34318652 -rw-r--r--. 1 root root 11476 6月  14 10:26 QueryResult.java
51253816 -rw-r--r--. 1 root root 22853 7月   8 13:56 sales_order.java
51265292 drwxr-xr-x. 3 root root    42 6月  15 17:05 ${system:java.io.tmpdir}
33696874 -rw-------. 1 root root  1675 6月  11 16:40 y
33696875 -rw-r--r--. 1 root root   393 6月  11 16:40 y.pub

可以看到,定義了命令別名之後,可以使用別名。
注意:命令別名與本地環境變量相似,它們只對於定義範圍內的shell進程有效:

[root@hadoop ~]# alias li='ls -li'
[root@hadoop ~]# bash
[root@hadoop ~]# li
bash: li: 未找到命令

3、如何使命令別名全局有效
在啓動新的交互式shell時,bash shell 始終會讀取HOME/.bashrcHOME/.bashrc啓動文件,可以將別名放入HOME/.bashrc

[root@hadoop ~]# cat .bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi

alias li='ls -li'
[root@hadoop ~]# bash
[root@hadoop ~]# li
總用量 112
33574979 -rw-------. 1 root root  1419 6月  11 13:33 anaconda-ks.cfg
51251036 -rw-r--r--. 1 root root 22255 7月   8 13:53 customer.java
19751199 -rw-r--r--. 1 root root 20640 7月   8 13:53 date_dim.java
34855665 -rw-r--r--. 1 root root     0 7月   8 20:38 derby.log
51251011 -rw-r--r--. 1 root root 14215 7月   7 14:55 product.java
34318652 -rw-r--r--. 1 root root 11476 6月  14 10:26 QueryResult.java
51253816 -rw-r--r--. 1 root root 22853 7月   8 13:56 sales_order.java
51265292 drwxr-xr-x. 3 root root    42 6月  15 17:05 ${system:java.io.tmpdir}
33696874 -rw-------. 1 root root  1675 6月  11 16:40 y
33696875 -rw-r--r--. 1 root root   393 6月  11 16:40 y.pub
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章