16

Is it possible to run my scripts located in ~/scripts from anywhere?

Say i want to run ./scriptname regardless of my current working directory.

0

3 Answers 3

19

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.

18

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.

2
  • +1 Well put answer. I usually add my bin at the front of the PATH. This way I can use my „own version” of commands. And as a side note, you don't need the trailing slash after the directory name.
    – lgarzo
    Jun 19, 2012 at 20:26
  • Thanks! You are right I removed the slashes in my comment. I wanted to provide a working solution.
    – Pascal
    Jun 19, 2012 at 20:36
2

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
1
  • Is this correct string, to add to PATH? /my/path/myscript.sh
    – Dr.jacky
    Apr 25, 2016 at 7:40

Not the answer you're looking for? Browse other questions tagged .