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?
According to the Zsh Workshop Aliases they seem to have the same syntax, so they should work.
The syntax of the
alias command is the same in all Bourne-style shells. So you can share them across
~/.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
~/.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 (
-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
alias allows global aliases, whereas
bashs are only expanded at the beginning of the line.
alias -g L="| less -FRX"
You can then do:
See here for a list of helpful global aliases.