drbunsen
drbunsen

Reputation: 10709

Is there a way to define a common alias to different paths for different computers in the Z-shell?

I have a Z-shell configuration that I use on multiple servers and my personal computer. On my personal computer I have an alias to an executable we will call foo. Here is the line in my zshrc to alias to this program:

alias foo=/path/to/foo

On several servers the path to foo is different and I can not move it to a common directory like ~/bin:

alias foo=/sever/path/to/foo

I need the alias to be foo on all machines, so I cannot create two seperate alias like foo and foo-server for example. Is there a way to have the Z-shell detect what machine I am on and change the alias to foo automatically? Naively, something like:

if on personal computer:
alias foo=/path/to/foo
else:
alias foo=/sever/path/to/foo

Upvotes: 0

Views: 76

Answers (2)

drbunsen
drbunsen

Reputation: 10709

For anyone else that comes across this, the best solution I could find is:

#{{{ Alias foo if it is in a specific location
if [[ -x ="/path/to/foo" ]]; then
    alias foo='/path/to/foo'
fi

#}}}

Thanks to larsmans for suggesting the -x flag

Upvotes: 1

byrondrossos
byrondrossos

Reputation: 2117

You should not use anything zsh specific. You can use hostname (or domainname or use the variable hostname set by zsh) and check whether on your pc or server. Then branch. This avoids the problem created if the file /path/to/foo exists but is not the right one, which the larsmans answer creates.

Upvotes: 0

Related Questions