writting executable script
October 8th, 2006 mysurface Posted in Bash, Developer, chmod, file, python, sh, which | Hits: 10609 |
Shell script (sh), Bash script (bash), Python and perl are all scripting language. By default, to run a scripting file, for example Python script, you need to do this:
python myscript.py
Another example for shell script:
sh myscript.sh
But we make is executable and can execute by running directly like this
./myscript.py
If you place your script into directory that exported into your PATH, such as /usr/bin; you can run without specified ./
How to make your script executable that?
- Add the path of script interpreter at the first line of your script.
- Change mode to executable
Let say I want to make my python script executable, I add this entry at the first line of myscript.py:
#!/usr/bin/python
Then i change mode to executable:
chmod +x myscript.py
For shell script, add this entry:
#!/bin/sh
You can use “which” to check the real path of the interpreter, like this
which sh
Another advantage of adding “sha bang” (#!….) is the script will be recognize by file.
file myscript.sh
If you do not add “sha bang”, file will display
myscript.sh: ASCII text
But after you add that,
myscript.sh: Bourne shell script text executable
Live Chat!









October 8th, 2006 at 6:58 pm
We may use the
envinstead if we don’t know the exact location ofpython:#! /usr/bin/env pythonMore at:
python FAQ, python’s mailling list discussion