Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have 2 VMs. One is debian lenny and one is Ubuntu. There is a script by name "two" in different folders of debian (name "two" is same but the contents are little bit different in different folders) and when I go to that folder, I just have to type two and enter to make it run. When I copied the same script to Ubuntu, even after giving it execute permissions, it still does not run by simply entering two. I have to type ./two to make it work. Is there any way I can type two in their respective folders and the scrips will run without ./ ?

share|improve this question
5  
Please don't do this. If you do this, someone can create a booby trap by creating a directory with an executable called ls in it. Then if you go into their directory and type ls, you will run their program with whatever permission you have. You are required to type ./ for a reason. –  David Schwartz Feb 15 '13 at 15:16
    
@DavidSchwartz: This only applies if . is at the beginning of $PATH – not when it is at the end. –  grawity Feb 15 '13 at 15:24
    
@grawity: It applies either way. For example, a lot of people type dir out of habit and then ls when they get an error. If someone knows you do this, they can create a program called dir. –  David Schwartz Feb 15 '13 at 16:16
    
@DavidSchwartz: It's a good thing dir is also part of coreutils. But I see your point. –  grawity Feb 15 '13 at 18:22
    
@grawity, a prank we used to play on unsuspecting victims with such a PATH was to add a script which echoed rm -rf * and waited a while in /tmp, under typical typo names like sl, pc, cgg... –  vonbrand Feb 15 '13 at 19:28
add comment

3 Answers

up vote 1 down vote accepted

You could add current dir to your path:

PATH="$PATH:."

With this current dir will always be in the shell search path.

You can persist this putting the code above in your .bashrc or .bash_profile

share|improve this answer
    
I have .bashrc file but it has no reference of PATH. How do I add multiple folders in the .bashrc by your way with . ? –  Samir Sogay Feb 17 '13 at 18:00
    
usually PATH is set in /etc/profile. That file is loaded by shell when user login. So when you do PATH="$PATH:.", the PATH variable is already loaded. If you need more folders you just have to add the pathnames in attribution string separated by colon. Ex: PATH="$PATH:/path/to/dir1:/path/to/dir2:.". –  Bruno Coimbra Feb 20 '13 at 18:44
    
I have added the folders in .profile but it is not taking effect. How do I add it in /etc/profile as there is no place to add it in the file? –  Samir Sogay Feb 22 '13 at 13:57
    
@SamirSogay, what is your shell? Usually, each shell loads its own initialization files. So if you are in Bash, it will try the follow load order: ~/.bash_profile, ~/.bash_login, ~/.profile and will load the first one found (from Bash manual). The file /etc/profile is loaded only if Bash is called with --login option and this happens firstly. As you said you are using Ubuntu, could you check if you really are using Bash? If I'm right, Ubuntu's default shell is Dash. –  Bruno Coimbra Jul 15 '13 at 13:29
add comment

You have to add the program/directory into your path.

A path set in .bash_profile will only be set in a bash login shell (bash -l). If you put your path in .profile it will be available to your complete desktop session. That means even metacity will use it.

For example ~/.profile:

if [ -d "$HOME/bin" ] ; then
  PATH="$PATH:$HOME/bin"
fi

Btw, you can check the PATH variable of a process by looking at its environment in /proc/[pid]/environ. (replace [pid] with the number from ps axf)

share|improve this answer
    
And for the path of your active shell you can just do echo $PATH. –  Hennes Feb 15 '13 at 15:33
    
There are references for bash and private binaries in the .profile file. How do I add /home/ubuntu/TS1 and /home/ubuntu/TS2? I don't understand from your example of home/bin. –  Samir Sogay Feb 17 '13 at 17:57
add comment

Instead of adding . to the PATH you might be better off adding the directory where "two" resides. For instance if two is really /usr/local/bin/two then you'd say

PATH=$PATH:/usr/local/bin

And if you may have lots of different programs like two then you amy want to place them all in a common directory instead of having to add a new directory to the path for each one.

share|improve this answer
    
I used your method of PATH=$PATH but when I opened another terminal, it does not show my path in echo $PATH. –  Samir Sogay Feb 17 '13 at 17:58
    
Yes, that command will only be in effect for that particular login. To make it permanent you could add it in your ~/.profile file as in krug's answer. –  Keith Wolters Feb 18 '13 at 3:17
    
So should I remove the home/bin from .profile and replace it with my folder or is there some way to put multiple folders? –  Samir Sogay Feb 18 '13 at 18:40
    
your PATH may include as many directories as you wish, each separated by ':'. –  Keith Wolters Feb 19 '13 at 16:26
    
I have added all my folders in .profile but none are shown in the echo $PATH. I have even rebooted my system. Am I doing anything wrong? OR should I do these changes in /etc/profile? –  Samir Sogay Feb 22 '13 at 13:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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