How to configure .zshrc for specfic os

I also share my Zsh startup between multiple operating systems. You could use a case statement for those commands which are system-specific:

case `uname` in
  Darwin)
    # commands for OS X go here
  ;;
  Linux)
    # commands for Linux go here
  ;;
  FreeBSD)
    # commands for FreeBSD go here
  ;;
esac

Alternatively you can split off system-specific startup into files called (say) .zshrc-Darwin, .zshrc-Linux, etc., and then source the required one near the end of your .zshrc:

source "${ZDOTDIR:-${HOME}}/.zshrc-`uname`"

Checking environment variable $OSTYPE is preferred, cause it's more lightweighted compared with running the external command uname.

OSTYPE is set by ZSH the shell itself.

OSTYPE

The operating system, as determined at compile time.

http://zsh.sourceforge.net/Doc/Release/Parameters.html#Parameters-Set-By-The-Shell

Example

# for ZSH
case "$OSTYPE" in
  darwin*)
    # ...
  ;;
  linux*)
    # ...
  ;;
  dragonfly*|freebsd*|netbsd*|openbsd*)
    # ...
  ;;
esac

Reference

  • Mature usage of OSTYPE in zsh's source code
  • Use "$OSTYPE" instead of "$(uname)" for OS detection in Bash

Note: Available OSTYPE values in Bash are a little different with the values in ZSH.


Just check if you are not running linux. If uname does not exist in Mac, the if clause will fail too.

if [ "$(uname 2> /dev/null)" != "Linux" ]; then
    alias emacs='vim'
fi

Tags:

Zsh