Live Chat!

writting executable script

          0 votos

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?

  1. Add the path of script interpreter at the first line of your script.
  2. 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

One Response to “writting executable script”

  1. We may use the env instead if we don’t know the exact location of python:

    #! /usr/bin/env python

    More at:
    python FAQ, python’s mailling list discussion

Leave a Reply