bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: #!path


From: Greg Wooledge
Subject: Re: #!path
Date: Thu, 27 Mar 2014 13:32:26 -0400
User-agent: Mutt/1.4.2.3i

On Thu, Mar 27, 2014 at 08:19:39AM -0700, esoj wrote:
> I need to specify a variable path after #! but seems to me that bash can't
> do this.
> For example I need to specify the $HOME or ~ path as in:
> 
> 
> #!~/bin/python
> or
> #!$HOME/bin/python

This belongs on help-bash, not bug-bash.  That said, you can't do this
directly with the shebang, because the kernel doesn't care about ~ or
$HOME.  It just knows file paths.

(That bit in the manual about "the shell does it if the OS doesn't"
is just a fallback for some really strange systems.  On almost any
system you are likely to use, the kernel handles the shebang, not bash.
In fact, bash would only be able to handle it if you were running the
program FROM bash in the first place.)

The typical workaround is to write some clever boilerplate code at the
top of the script which is interpreted a comment (or a no-op) in one
language, and a valid instruction (to reinvoke the program with a
different interpreter) in another language.  In this case, you want the
shebang to be #!/bin/sh, and you want boilerplate code that invokes
the script under ~/bin/python when the script is run by /bin/sh, but
which does nothing when it's invoked by python.

I would imagine some Python FAQ has this somewhere... let's see what
Google turns up.

http://softwareswirl.blogspot.com/2011/06/starting-python-script-intelligently.html
http://stackoverflow.com/questions/19304295/make-usr-bin-env-python-u-shebang-portable

There, that's not bad.  The first answer in the second URL is nice
and short.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]