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

12,962

Solution 1

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.

Solution 2

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 "$@"; }

Solution 3

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

Solution 4

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,

Share:
12,962

Related videos on Youtube

hpekristiansen
Author by

hpekristiansen

Casual LaTeX user. TikZ user.

Updated on September 18, 2022

Comments

  • hpekristiansen
    hpekristiansen almost 2 years

    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
    hpekristiansen over 12 years
    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:/u‌​sr/texbin:/usr/X11/b‌​in What would be a good place for these links?
  • HikeMike
    HikeMike over 12 years
    @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.
  • HikeMike
    HikeMike over 12 years
    The first open command should be open -a Aquamacs. Chances are the open -a octave "$@" examples are missing --args.
  • hpekristiansen
    hpekristiansen over 12 years
    What does "$@" mean, and where do I read about it?
  • HikeMike
    HikeMike over 12 years
    @Hans-PeterE.Kristiansen "$@" is all arguments to the function or script in order, individually quoted. See man bash in the section Special Parameters.
  • Olivier.Roger
    Olivier.Roger about 4 years
    As described, this answer needs more info. There are a couple issues you can run into if you try it this way. To keep it simple, I would prefer to use alias command along with an alias to the open command.