In a Unix-like system, shebang precedes the interpreter directive which specifies the interpreter program to be run. The following examples are based on Python 3, including the Anaconda 3 interpreter. If the Anaconda3 Python interpreter is installed, when the user enters:
which python3
The terminal emulator returns:
/root/anaconda3/bin/python3
The example above helps the user hard code a local interpreter directive, which is less flexible than a global directory path. At the very first line of the script file, simply add a Unix shebang before appending the path:
#!/root/anaconda3/bin/python3
env Shell Command
The following demonstrates a more flexible global example:
#!/usr/bin/env python3
One purpose of the env shell command is to execute a program in a modified environment. In this case, python3. env searches the PATH environmental variable for Python’s directory path in a Unix-like operating system—OS X, Linux, etc.
Unix Dot Slash
As an example, in a unix-like terminal emulator, if the Python script is based on Python 3.5, and the interpreter directive is:
#!/usr/bin/python3.5
Then the following will allow that shell script to self execute in MacOS or Linux:
./foo.py
To self execute a shell script, the user will likely need to configure executable permission, e.g. chmod a+x foo.py
. Also, the ./
prefix specifies current directory.
Your Turn
In the terminal emulator of a Unix-like system, try entering:
which python2