writting executable script

October 8th, 2006 mysurface Posted in Bash, chmod, Developer, file, python, sh, which | Hits: 80451 | 9 Comments »

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

9 Responses 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

  2. how can I write an executable script using the linux that computes summary statistics for the variables x1,x2,and x3. Include: n, no. of missing observations, mean, variance, standard deviation, and minimum and maximum values.

  3. how to compute correlattions among three variables?

  4. Interesting read i think your website is fantastic with top notch content which i like to add to my bookmarks. I’d like to share everyone this new type of app that allows you to spy on other peoples iphone pretty clever if you ask me check out cell phone spy

  5. writting executable script » Linux by Examples I was recommended this website by my cousin. I’m not sure whether this post is written by him as nobody else know such detailed about my difficulty. You are amazing! Thanks! your article about writting executable script » Linux by ExamplesBest Regards Lisa

  6. How come you do not have your site viewable in wap format? Can not view anything in my Droid.

  7. There is obviously a bunch to know about this. I believe you made certain nice points in features also.

  8. Heya i am for the first time here. I found this board and I
    find It truly useful & it helped me out a lot. I hope to give something back and aid others like you
    aided me.

    best rated casinos in southern california [Laurinda]

  9. Hi i am kavin, its my first occasion to commenting anyplace, when i read this piece of writing
    i thought i could also create comment due to this good
    paragraph.

Leave a Reply