How to configure .zshrc for specfic os

I want to use emacs from Applications folder when I’m using Mac, but I’m using same .zshrc in Ubuntu.

alias emacs='/Applications/Emacs.app/Contents/MacOS/bin/emacsclient'

So I want to create this alias for only when I’m using OS X. How can I get a OS name in .zshrc?

Asked By: ironsand

||

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
Answered By: Kira

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`"
Answered By: wjv

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

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

Answered By: Simba
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.