Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a bash script which I would to like to execute from everywhere (like other Linux basic commands, for example ls). I tried to follow this tutorial but it doesn't work, I always get command not found.

Also would be nice if there is a way how to execute directly Python script in this way, currently I have .py file with the script and then file .sh which calls python /path/to/it.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

If you write a python script that you can execute it in at least the following three ways:

1 Start python with the script as an argument.

Example: /usr/local/bin/python-2.5 myscript.py

2 Start the script with shebang interpreter.

This means that the very first line starts with #! (also called a shebang) followed by a space (or most often not!) and the full path to the interpreter.

Example:

#!/usr/local/bin/python-2.5
#
# My test script!
#
Do something

Notice that I added comments after the first line. Do not add them above it since then the first line no longer starts with a shebang.

3 Start the script with #!/usr/bin/env python

This is more or less the same as option 2, except that env will find python for you. If python ever moves to a different location or a new version then you do not have to update all your scripts.

This is the preferred solution


So much for the part where you used a sh script to call python /path/to/it. You now now how to skip that part.

The second part of your question seems that your script is not found. To find a script or program you either need to specify the full path to it (e.g. /usr/local/bin/myscript.py` or it needs to be in your path.

You can check what your current path is set to with echo $PATH.

It probably includes /usr/local/bin/, which means that any program or script dropped in the directory /usr/local/bin/ will be found. If it does not add that path to the system wide default. (Note: start a login shell to test this after changing this. Changing the config file without starting a new shell will not reflect the changes).

Lastly I skipped the part where you want the script to be executable. You already had that in the tutorial you linked you.

share|improve this answer
    
Thanks, that works, great explanation! –  Petr Mensik Jan 15 '13 at 20:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.