在 .bash_profile 中有没有办法创建两个执行相同操作的别名?

Is there a way in .bash_profile, to create two aliases that do the same thing?

我的 .bash_profile 中有一些简单的别名(OS X El Capitan,10.11.6)。有时,我想要多个别名来做同样的事情。例如,我有一个文件夹,它是我编程项目的起点。目前,我有一个代码别名和一个做同样事情的编程别名:

alias code='cd /path/to/Programming/; clear; pwd; ls'
alias programming='cd /path/to/Programming/; clear; pwd; ls'

这让我不必记住我是如何为这个目录起别名的。有没有办法设置多个别名而不必重复命令?简而言之,有这样的东西吗?

alias code,programming='cd /path/to/Programming/; clear; pwd; ls'

你可以这样做:

alias foo='ls'   # Arbitrary command. I've used ls for example
alias bar='foo'  # Use alias from previous definition

这也可以是一行:

alias foo='ls' bar='foo'

要了解更多信息,请在 shell 中输入 help alias


顺便说一句,我会将这些别名放入 .bashrc 而不是 .bash_profilehttps://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html#Bash-Startup-Files

你可以使用

alias code='cd /path/to/Programming/; clear; pwd; ls'
alias programming='code'

甚至在单个 alias 语句中进行多个赋值,请参阅

展开别名时,会再次检查第一个词是否为别名,因此您可以 "chain" 它们,请参阅 the manual:

The first word of the replacement text is tested for aliases, but a word that is identical to an alias being expanded is not expanded a second time.

这允许像

这样的别名
alias ls='ls -F'

没有无限递归。

您只能为每个实体提供一个名称,但您可以在同一命令中将一个名称定义为另一个名称的别名。例如,

alias code='cd /path/to/Programming/; clear; pwd; ls' programming=code

在实践中,我建议为 "real" 函数定义一个函数而不是别名。

code () {
  cd /path/to/Programming
  clear
  pwd
  ls
}

alias programming=code

在不相关的切线上,zsh 允许为单个函数定义多个名称:

code programming () {
  cd /path/to/Programming
  clear
  pwd
  ls
}

In short, is there something like this?

alias code,programming='cd /path/to/Programming/; clear; pwd; ls'

是的,大括号展开:

alias {code,programming}='cd /path/to/Programming/; clear; pwd; ls'