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

Viewed 1556

I have some simple aliases in my .bash_profile (OS X El Capitan, 10.11.6). Sometimes, I want multiple aliases that do the same thing. I have, for example, a folder that is my starting point for programming projects. Currently, I have an alias for code and one for programming that do the same thing:

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

This saves me having to remember how I aliased this directory. Is there a way to set multiple aliases without having to repeat the command? In short, is there something like this?

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

In short, is there something like this?

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

Yes, brace expansion:

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

You can only provide one name per body, but you can define one name to be an alias to the other in the same command. For example,

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

In practice, I recommend defining a function instead of an alias for the "real" one.

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

alias programming=code

On an unrelated tangent, zsh does allow defining multiple names for a single function:

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

You could use

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

or even multiple assignments in a single alias statement, see chepner's answer.

When expanding an alias, the first word is checked for being an alias again, so you can "chain" them, see 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.

This allows for aliases like

alias ls='ls -F'

without infinite recursion.

Related