How can I make script globally executable in Ubuntu?

6,316

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:
6,316

Related videos on Youtube

Petr Mensik
Author by

Petr Mensik

I mostly do Java and Java EE stuff although I am currently also interested in security and big data. SOreadytohelp

Updated on September 18, 2022

Comments

  • Petr Mensik
    Petr Mensik over 1 year

    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.