2

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?

4 Answers 4

7

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.

4
  • 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? Feb 5, 2012 at 21:34
  • 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
    Feb 5, 2012 at 21:38
  • @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
    Feb 5, 2012 at 21:46
  • 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.
    – djangofan
    Jul 4, 2020 at 18:24
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 "$@"; }
3
  • The first open command should be open -a Aquamacs. Chances are the open -a octave "$@" examples are missing --args.
    – Daniel Beck
    Feb 5, 2012 at 21:40
  • What does "$@" mean, and where do I read about it? Feb 5, 2012 at 22:03
  • @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
    Feb 5, 2012 at 22:21
2

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

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,

You must log in to answer this question.

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