I have searched around but could not find anything conclusive. Is there a difference between the alias command in zsh and the alias command in bash? If not, does it mean I can share a set of aliases between the two shells and expect them to work as I intended?
3 Answers
The syntax of the alias
command is the same in all Bourne-style shells. So you can share them across ~/.zshrc
, ~/.bashrc
, ~/.kshrc
, ~/.shrc
, as long as they make sense in all the shells.
The same holds for variable definitions and function definitions, as long as you use the subset of syntax that's supported in all shells.
If you don't use versions of zsh older than 4.0, you can put all your shell-agnostic definitions in a file called (say) ~/.common.rc.sh
, where the first line is
emulate -LR sh 2>/dev/null
This tells zsh to expect sh compatible syntax in this file only. Then source that file near the beginning of ~/.bashrc
, ~/.bashrc
, ~/.kshrc
and so on.
This is basically what I do. For example, I have somewhat complex code that generates an alias for ls
with my favorite options depending on what's available (--color
, -G
, -F
, etc.); it's shell-agnostic, so goes in .common.rc.sh
. I also have shell-dependent aliases, like alias zcp='zmv -C'
that goes in .zshrc
.
-
1If I put a
/bin/sh
shebang at the top of my aliases file will it work as I intend for all shells? Commented Nov 25, 2011 at 13:08 -
1@ZameerManji No, don't put a
#!
line: this is not a standalone script, it can only be sourced in another script.#!
doesn't change the shell's behavior anyway. Commented Nov 25, 2011 at 13:33 -
3A tiny difference I found is that zsh's
alias
treats an argument beginning with+
as introducing an option like-
would, soalias +=something
must be written asalias -- +=something
in zsh. Don't thinkemulate
changes that.– MikelCommented May 11, 2012 at 1:09 -
Be careful with
~/.shrc
since the Bourne Shell has the most powerful alias implementation butzsh
uses incompatible option names.alias -g
creates a persistent alias in$HOME/.globals
with the Bourne Shell. Sharing alias definitions is only safe as long as you use no more than the POSIX features.– schilyCommented Jul 2, 2018 at 8:01
zsh
's alias
allows global aliases, whereas bash
s are only expanded at the beginning of the line.
In zsh
:
alias -g L="| less -FRX"
You can then do:
verbose-command L
See here for a list of helpful global aliases.
-
Calling all expand aliases "global" aliases is a really bad idea since there is a concept of local aliases that are restricted to a specific directory while global aliases since 38 years apply to aliases that are diectory independent. This concept is from 1980 from UNOS and implemented in the Bourne Shell since 6 years. UNOS introduced all expand aliases in 1980 as well, zsh could have done some research for avoiding this type of clash. See schillix.sourceforge.net/man/man1/bosh.1.html– schilyCommented Jul 2, 2018 at 7:52
alias
alone will list your aliases in sourcable command form, while in zsh you need to use the-L
option to get that.