制作 bash 别名-ag捕鱼王app官网

当前位置:ag捕鱼王app官网 > > 操作系统 >

制作 bash 别名

作者:迹忆客 最近更新:2024/03/15 浏览次数:

别名是 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 已从别名列表中删除。

上一篇:暂停 bash shell 脚本 x 秒

下一篇:没有了

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

发布时间:2024/03/14 浏览次数:96 分类:操作系统

本文介绍了如何暂停程序在 linux bash 中的执行。

发布时间:2024/03/14 浏览次数:76 分类:操作系统

本文演示了如何在 linux bash 脚本中使用 test 命令和 -z 标志进行比较。

发布时间:2024/03/14 浏览次数:127 分类:操作系统

本文将演示如何使用 find 命令的 -exec 参数来使用 find 命令定位文件中的任何文本。

发布时间:2024/03/14 浏览次数:288 分类:操作系统

本教程演示了在 bash 中生成随机数。

发布时间:2024/03/14 浏览次数:176 分类:操作系统

本教程演示了在 bash 中模拟一个 do-while 循环。

发布时间:2024/03/14 浏览次数:182 分类:操作系统

如何在 bash 中解析 json 数据

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图