How do I install a script to run anywhere from the command line?

Posted on

Question :

How do I install a script to run anywhere from the command line?

If I have a basic Python script, with it’s hashbang and what-not in place, so that from the terminal on Linux I can run

/path/to/file/MyScript [args]

without executing through the interpreter or any file extensions, and it will execute the program.

So would I install this script so that I can type simply

MyScript [args]

anywhere in the system and it will run? Can this be implemented for all users on the system, or must it be redone for each one? Do I simply place the script in a specific directory, or are other things necessary?

Asked By: jeffbr13


Answer #1:

The best place to put things like this is /usr/local/bin.

This is the normal place to put custom installed binaries, and should be early in your PATH.

Simply copy the script there (probably using sudo), and it should work for any user.

Answered By: agf

Answer #2:

Walkthrough of making a python script available anywhere:

Make a python script:

cd /home/el/bin
chmod +x

Find out where your python is:

which python

Put this code in there:

print "hi"

Run in it the same directory:


Go up a directory and it’s not available:

cd ..

-bash: command not found

Not found! It’s as we expect, add the file path of the python file to the $PATH

vi ~/.bashrc

Add the file:

export PATH=$PATH:/home/el/bin

Save it out, re apply the .bashrc, and retry

source ~/.bashrc

Try again:

cd /home/el



The trick is that the bash shell knows the language of the file via the shebang.

Answered By: Eric Leschinski

Answer #3:

The quick answer is to symlink your script to any directory included in your system $PATH.

The long answer is described below with a walk through example, (this is what I normally do):

a) Create the script e.g. $HOME/Desktop/

print("Hello Pythonista!")

b) Change the permission of the script file to make it executable:

$ chmod +x

c) Add a customized directory to the $PATH (see why in the notes below) to use it for the user’s scripts:

$ export PATH="$PATH:$HOME/bin"

d) Create a symbolic link to the script as follows:

$ ln -s $HOME/Desktop/ $HOME/bin/hello

Notice that hello (can be anything) is the name of the command that you will use to invoke your script.


i) The reason to use $HOME/bin instead of the /usr/local/bin is to separate the local scripts from those of other users (if you wish to) and other installed stuff.

ii) To create a symlink you should use the complete correct path, i.e.

$HOME/bin GOOD ~/bin NO GOOD!

Here is a complete example:

 $ pwd
 $ cat > << EOF
 > #!/usr/bin/python
 > print("Hello Pythonista!")
 > EOF
 $ export PATH="$PATH:$HOME/bin"
 $ ln -s $HOME/Desktop/ $HOME/bin/hello
 $ chmod +x
 $ hello
Hello Pythonista!
Answered By: Aziz Alto

Answer #4:

Just create ~/bin and put export PATH=$PATH:$HOME/bin in your bashrc/profile. Don’t mess with the system, it will bite you back, trust me.

Few more things (relevant to the question but not part of the answer):

  1. The other way export PATH=$HOME/bin:$PATH is NOT safe, for bash will will look into your ~/bin folder for executables, and if their name matches with other executables in your original $PATH you will be surprised by unexpected/non working command execution.
  2. Don’t forget to chmod+x when you save your script in ~/bin.
  3. Be aware of what you are putting in your ~/bin folder, if you are just testing something or working on unfinished script, its always better to use ./$SCRIPT_NAME from your CWD to execute the script than putting it under ~/bin.
Answered By: Sudhi

Answer #5:

Just create symbolic link to your script in /usr/local/bin/:

sudo ln -s /path/to/your/ /usr/local/bin/script
Answered By: Iurii Vasylenko

Answer #6:

Putting the script somewhere in the PATH (like /usr/local/bin) is a good solution, but this forces all the users of your system to use/see your script.

Adding an alias in /etc/profile could be a way to do what you want allowing the users of your system to undo this using the unalias command. The line to be added would be:

alias MyScript=/path/to/file/MyScript
Answered By: jfg956

Answer #7:

you can also use setuptools (

  1. your script will be:
def hi():

(suppose the file name is

  1. also add file next to your script (with nothing in it).

  2. add script, with the content:

#!/usr/bin/env python3

import setuptools

install_requires = [

        'console_scripts': [
            'cool_script = hello:hi',

  1. you can now run python develop in this folder
  2. then from anywhere, run cool_script and your script will run.
Answered By: Alon Gouldman

Answer #8:

Type echo $PATH in a shell. Those are the directories searched when you type command, so put it in one of those.

Edit: Apparently don’t use /usr/bin, use /usr/local/bin

Answered By: dcpomero

Leave a Reply

Your email address will not be published. Required fields are marked *