Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

This question already has an answer here:

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

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

share|improve this question

marked as duplicate by muru, Radu Rădeanu Nov 1 at 15:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers 3

up vote 4 down vote accepted

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.

share|improve this answer

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.

share|improve this answer
    
+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 '12 at 20:26
    
Thanks! You are right I removed the slashes in my comment. I wanted to provide a working solution. –  Pascal Jun 19 '12 at 20:36

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
share|improve this answer

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