8

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?

6

According to the Zsh Workshop Aliases they seem to have the same syntax, so they should work.

  • I can attest to that. The do for me. – frogstarr78 Nov 24 '11 at 5:17
13

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.

  • 1
    If I put a /bin/sh shebang at the top of my aliases file will it work as I intend for all shells? – Zameer Manji Nov 25 '11 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. – Gilles Nov 25 '11 at 13:33
  • 3
    A tiny difference I found is that zsh's alias treats an argument beginning with + as introducing an option like - would, so alias +=something must be written as alias -- +=something in zsh. Don't think emulate changes that. – Mikel May 11 '12 at 1:09
  • Be careful with ~/.shrc since the Bourne Shell has the most powerful alias implementation but zsh 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. – schily Jul 2 '18 at 8:01
1

zsh's alias allows global aliases, whereas bashs 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 – schily Jul 2 '18 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.