2010-05-22

avoid shebang in unix shells

On May 20, 12:01 pm, Johan Andersson wrote:
> Hey,
>
> I have an Emacs script where the first line looks like this:
> #!/usr/bin/emacs --script
>
> However, on Mac OSX, Emacs is installed by default, but with an old version.
>
> I have also installed Emacs via Homebrew (compiled from source) and can run
> that with:
> /Usr/local/Cellar/emacs/23.2/Emacs.app/Contents/MacOS/Emacs
>
> So, to run the script with the Homebrew version, I could change the first
> line in my script to:
> #!/Usr/local/Cellar/emacs/23.2/Emacs.app/Contents/MacOS/Emacs --script
>
> That works, but I want this script to work on both GNU/Linux and Mac OSX
> (Both default and Homebrew version).
>
> I was thinking I could use env somehow, like this:
> #!/usr/bin/env emacs --script
>
> And then make emacs an alias or function that points to the correct binary
> depending on system. But it's still the default Emacs that is used.
>
> Any ideas how I could solve this?

from my experiences, it's best to avoid the shebang construct. Just call the program directly.

The shebang construction is something of the 1980s. It's outdated and complex, and doesn't work well across platforms.

not sure what's your need here exactly, but for example, to call the script in mac, you can do as in your example:

/Usr/local/Cellar/emacs/23.2/Emacs.app/Contents/MacOS/Emacs --script myscript.el

to avoid typing the long text, you can define a alias. So, the whole thing can be either

emacs --script myscript.el

or even just

doit

to define a alias in bash, do like this:

alias doit='/Usr/local/Cellar/emacs/23.2/Emacs.app/Contents/MacOS/Emacs --script myscript.el'

Also, don't rely on environment variables if possible. They create more problems than is worth.

Xah
∑ http://xahlee.org/

No comments:

Post a Comment