When writing a plain text file for any various langauges like python, bash, R, etc. you can call the interperter directly on the file like below.
a_python_script.py
import sys
def hello(name):
sys.stdout.write("hello" + name + "\n")
name = "nick"
hello(name)
python a_python_script.py
hello nick
a_R_script.R
print("hello world")
Rscript a_R_script.R
[1] "hello world"
Or you can make the file executable using chmod and then add a line to the very top of your file called the she-bang line which supply the interperter.
Add she-bang line
a_python_script.py
#!/usr/bin/python
import sys
def hello(name):
sys.stdout.write("hello" + name + "\n")
name = "nick"
hello(name)
You have to also make the file executable, if you don’t you’ll see this line
./a_python_script.py
-bash: ./a_python_script.py: Permission denied
Make file executable, just has to be done one time
chmod +x a_python_script.py
Now you can just run the file and it will be interpreted by python
./a_python_script.py
hello nick
The she-bang always starts with #!
and should be on the very first line of the file. What fallows the #!
is the path to the program you want to have interpret the file. The above with giving the path /usr/bin/python
will probably work for most default systems but might not work on things like a large computer cluster where you have use lsf
and module load
. So a better way to get the right interperter is to use the program env
which will search your environment for the interperter that you give it ensuring the right interperter is found.
a_python_script.py
#!/usr/bin/env python
import sys
def hello(name):
sys.stdout.write("hello" + name + "\n")
name = "nick"
hello(name)
So instead of giving the direct path to the python interperter you can query your environment for python
and env
will look at your PATH
to find the correct one, this will also help to find programs if you have them instead in non-default places. Also using the /usr/bin/env python
will make it easier to port your code around, giving it a higher chance of working when you use it on default machines and setups.
You can do the same for any program like Rscript as well
a_R_script.R
#!/usr/bin/env Rscript
print("hello world")