Launch shell scripts from anywhere [duplicate]

Solution 1:

You can add ~/scripts to your $PATH environment variable. Then you can run scriptname from anywhere (but not ./scriptname, because the ./ denotes the current directory).

This answer shows how to add ~/bin to $PATH, but you can do the same with ~/scripts of course.

Solution 2:

Add

 export PATH=$PATH:~/scripts

to your the end of your ~/.bashrc file. This will allow you to execute your scripts in ~/scripts/ by simply typing scriptname in the bash. You need to logout in order for it to work in your session (you can test the scripts by opening a new terminal).

I usually add

 export PATH=$PATH:~/bin

to my path and then create symlinks to the scripts an programs I want to have available in my session.

cd ~/bin
ln -s ~/Tools/eclipse3.7/eclipse 
# which will create a symlink in ~/bin/ with the name eclipse
# pointing to ~/Tools/eclipse3.7/eclipse which allows me to execute
# the eclipse in ~/Tools/eclipse3.7/

Note that the path files have a precedence. If I already have installed eclipse through ubuntu, it will first search eclipse in all other places than in ~/bin/. If you want to change this behavior just export the directory the other way around:

export PATH=~/bin:$PATH

Make sure the scripts in your scripts directory are executable otherwise the export won't have an effect.

You can look at the available paths by typing

echo $PATH

If you want to see all environment variables type env in your console.

Solution 3:

Two ways:

  • call your shell scripts with a full path instead of ./, like ~/scripts/scriptname
  • add the folder that contains your shell scripts to the $PATH environment variable and call your shell scripts without the leading ./, like scriptname