What is the correct way to alias applications in OS X through bash?

2

1

In my ~/.bashrc, I have several aliases like:

alias emacs='/Applications/Aquamacs.app/Contents/MacOS/Aquamacs'
alias octave='/Applications/Octave.app/Contents/Resources/bin/octave'
alias wine='/Applications/Wine.app/Contents/Resources/bin/wine'
alias simion='wine "/Users/hpek/.wine/drive_c/Program Files/SIMION 8.0/simion.exe"'
alias inkscape='wine "/Users/hpek/.wine/drive_c/Program Files/Inkscape/inkscape.exe"'

I do not think that this is the right way to do it. The aliases does not work from within bash scripts, and when installing something through brew or apt-get, it does not create an alias like this.

What is the correct way to do this?

hpekristiansen

Posted 2012-02-05T21:26:24.850

Reputation: 209

Answers

6

Aliases don't work in shell script by design. Otherwise e.g. alias rm='rm -i' will break most shell scripts.

To enable them anyway, set the expand_aliases shell option.


You can create softlinks for these executables in a directory on your $PATH:

ln -s /Applications/Aquamacs.app/Contents/MacOS/Aquamacs /usr/bin/aquamacs

Then just type the new command name, e.g. aquamacs, to run them.

This will allow use of these commands independent of your shell.


Note that for regular OS X applications, a non-blocking way to open them is open -a ProgramName, e.g. open -a Aquamacs. It uses Launch Services' program database (the one e.g. providing the selection of programs for opening a certain file with a non-default editor) and knows where the applications are installed.

Daniel Beck

Posted 2012-02-05T21:26:24.850

Reputation: 98 421

Is that how homebrew/apt-get does it? where are these links? echo $PATH gives: /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/texbin:/usr/X11/bin What would be a good place for these links? – hpekristiansen – 2012-02-05T21:34:02.923

1@Hans-PeterE.Kristiansen Homebrew/apt-get install to these directories, usually /usr/bin or /usr/local/bin. Homebrew creates /usr/local/bin and adds it to the PATH, so you can run the programs without typing their full name. With Homebrew, there's some additional link creation going on as well, so in that regard it's similar. I'd place the links in /usr/bin (as shown in the example) or /usr/local/bin, since you already have it. It's possible you need to prefix the ln command with sudo to be able to write to these directories. – Daniel Beck – 2012-02-05T21:38:17.870

@Hans-PeterE.Kristiansen You won't see Homebrew installed programs in /Applications. They are stored in subdirectories of /usr/local. Run /usr/local/bin to see the links created for them. – Daniel Beck – 2012-02-05T21:46:13.457

3

That is the right way to do it, just for a different value of "it" to what you want to achieve. (alias is just for interactive use, pretty much.)

The way to open an app bundle on the Mac is to use open -a ${appname}, so you could / should replace your emacs alias with alias emacs='open -a aquamacs', and your inkscape with open -a wine '/Users/hpek/.wine/...'.

The way that you have a directly executable emacs binary, though, is to have something named literally that on your $PATH. I tend toward tiny scripts:

#!/bin/sh
exec /usr/bin/open -a octave "$@"

That opens the application the "mac" way, passes all the command line arguments through it, and is executable from anything including other processes.

If you only care about the shell, though, shell functions are more "normal" than aliases, in that they can be triggered in more circumstances in bash:

function octave() { /Applications/Octave.app/.../bin/octave "$@"; }
function octave() { open -a octave "$@"; }

Daniel Pittman

Posted 2012-02-05T21:26:24.850

Reputation: 3 510

The first open command should be open -a Aquamacs. Chances are the open -a octave "$@" examples are missing --args. – Daniel Beck – 2012-02-05T21:40:16.027

What does "$@" mean, and where do I read about it? – hpekristiansen – 2012-02-05T22:03:39.093

@Hans-PeterE.Kristiansen "$@" is all arguments to the function or script in order, individually quoted. See man bash in the section Special Parameters. – Daniel Beck – 2012-02-05T22:21:42.060

1

Let's say I have a executable called python3.6 inside the folder:

/Users/doekewartena/.pyenv/versions/3.6.0/bin/

Then I can do:

alias python36='/Users/doekewartena/.pyenv/versions/3.6.0/bin/./python3.6'

NOTICE the /./ at the end,

clankill3r

Posted 2012-02-05T21:26:24.850

Reputation: 134

1

Simple solution is to add the following command in .bash_profile:

alias rstudio='open -a /Applications/RStudio.app/Contents/MacOS/RStudio "$@"'

This will allow the program RStudio to open any existing .R file passed to the command

naturofix

Posted 2012-02-05T21:26:24.850

Reputation: 11