1

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 ./ ?

5
  • 6
    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. Feb 15, 2013 at 15:16
  • @DavidSchwartz: This only applies if . is at the beginning of $PATH – not when it is at the end. Feb 15, 2013 at 15:24
  • 1
    @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. Feb 15, 2013 at 16:16
  • @DavidSchwartz: It's a good thing dir is also part of coreutils. But I see your point. Feb 15, 2013 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, 2013 at 19:28

3 Answers 3

1

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

4
  • I have .bashrc file but it has no reference of PATH. How do I add multiple folders in the .bashrc by your way with . ? Feb 17, 2013 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:.". Feb 20, 2013 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? Feb 22, 2013 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. Jul 15, 2013 at 13:29
3

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)

2
  • And for the path of your active shell you can just do echo $PATH.
    – Hennes
    Feb 15, 2013 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. Feb 17, 2013 at 17:57
1

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.

5
  • I used your method of PATH=$PATH but when I opened another terminal, it does not show my path in echo $PATH. Feb 17, 2013 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. Feb 18, 2013 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? Feb 18, 2013 at 18:40
  • your PATH may include as many directories as you wish, each separated by ':'. Feb 19, 2013 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? Feb 22, 2013 at 13:51

You must log in to answer this question.

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