22

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?

3 Answers 3

28

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`"
1
  • 1
    Thanks a lot, this is exactly what I was searching for Nov 5, 2020 at 10:04
16

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.

0
3

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
1
  • uname exists on Mac. Mac is POSIX-compatible. Apr 20, 2023 at 14:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .