bug-bash
[Top][All Lists]
Advanced

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

Re: #!path


From: Jose Soares
Subject: Re: #!path
Date: Fri, 28 Mar 2014 08:53:08 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121028 Thunderbird/16.0.2

Hi Greg,
The links you pointed me are great. They were exactly what I was looking for. Thanks a lot.:-)
j

On 03/27/2014 06:32 PM, Greg Wooledge wrote:
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]