制作 Bash 别名

Nilesh Katuwal 2023年1月30日
  1. 在 Bash 中创建别名
  2. 创建带参数的 Bash 别名
  3. 删除 Bash 中的别名
制作 Bash 别名

别名是 shell 中的一个命令,它允许将一个单词替换为另一个字符串。它主要用于缩短系统命令或为常用命令提供默认参数。

它类似于快捷命令,具有与编写整个命令相同的功能。

在 Bash 中创建别名

让我们看一个例子。

$ alias update="sudo apt-get update"

我们创建了一个别名 update,它是 sudo apt-get update 的快捷方式。现在,当我们运行 update 时,它的工作方式与 sudo apt-get update 相同。

$ update

输出:

[sudo] password for username: 
Get:1 https://typora.io/linux ./ InRelease [793 B]            
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]                                                                            
Hit:3 http://np.archive.ubuntu.com/ubuntu focal InRelease                                                                                            
Hit:4 http://ppa.launchpad.net/micahflee/ppa/ubuntu focal InRelease                                  
Ign:5 http://linux.dropbox.com/ubuntu disco InRelease                                                
Get:6 http://np.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]                                                                           
Hit:7 http://linux.dropbox.com/ubuntu disco Release                                                                                                  
Ign:8 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 InRelease                                                                            
Hit:10 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 Release                                                                             
Get:12 http://security.ubuntu.com/ubuntu focal-security/main amd64 DEP-11 Metadata [35.7 kB]                                                         
Get:13 http://np.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]                                                                        
Get:14 http://security.ubuntu.com/ubuntu focal-security/universe amd64 DEP-11 Metadata [66.3 kB]                                                     
Get:15 http://np.archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [278 kB]                                                         
Get:16 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 DEP-11 Metadata [2,468 B]                                                   
Get:17 http://np.archive.ubuntu.com/ubuntu focal-updates/universe amd64 DEP-11 Metadata [363 kB]                                                     
Get:18 http://np.archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 DEP-11 Metadata [940 B]                                                    
Get:19 http://np.archive.ubuntu.com/ubuntu focal-backports/main amd64 DEP-11 Metadata [7,996 B]                                                      
Get:20 http://np.archive.ubuntu.com/ubuntu focal-backports/universe amd64 DEP-11 Metadata [11.3 kB]                                                  
Fetched 1,102 kB in 9s (120 kB/s)                                                                                                                    
Reading package lists... Done

要列出所有别名,请运行以下命令。

$ alias

输出:

alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias echo='show'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
alias show='echo'
alias update='sudo apt-get update'

新创建的别名 update 也与所有其他别名一起在列表中。

创建带参数的 Bash 别名

Bash 别名不直接接受参数。我们必须写一个函数,因为函数机制的适应性更强,提供的功能和以前一样。

尽管别名不带参数,但可以以与别名相同的方式调用函数。让我们看一个例子。

$ alias wargs='f(){ echo first "$@" last;  unset -f f; }; f'
$ wargs a b c

在上面的示例中,创建了一个临时函数 f。参数在最后调用 f 时发送。

未设置的 -f 选项在执行别名时消除了函数定义,确保它不会在之后逗留。

输出:

first a b c last

删除 Bash 中的别名

我们可以使用 unalias 命令删除之前创建的别名 update

$ unalias update

让我们再次检查所有别名的列表。

$ alias

输出:

alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias echo='show'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
alias show='echo'
alias wargs='f(){ echo first "$@" last;  unset -f f; }; f'

如输出所示,别名 update 已从别名列表中删除。