Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preferred way of #! in shell scripts

Tags:

shell

I searched google but couldn't find an answer to this rather simple question. I have a python script that has the hash-bang (#!) on the first line:

#!/usr/bin/python

However, what if this is run on a computer with python in /bin/python or /usr/local/bin/python, or some other place? There has to be a better way to set the interpreter for a shell script. It should be possible to set it via $PATH, as that will know where to find python if it's installed on the system.

like image 624
Staale Avatar asked Feb 05 '09 12:02

Staale


3 Answers

Use env.

#!/usr/bin/env python

It's not bulletproof, but it covers more cases than /usr/bin/python.

like image 152
Philip Reynolds Avatar answered Nov 15 '22 08:11

Philip Reynolds


Use

#!/usr/bin/env python

env is virtually always in /usr/bin, and will execute any program in the PATH.

like image 40
phihag Avatar answered Nov 15 '22 08:11

phihag


Some people prefer to start with:

#!/usr/bin/env python

Not sure that this is a vast improvement as you're now assuming that python is in the path and that it's the right version, but it's an option.

like image 40
Stephen Darlington Avatar answered Nov 15 '22 06:11

Stephen Darlington