4

I had an executable script on my ubuntu located on ~/project/ directory and I tried to add that path to /etc/environment . So , I edit the path to this PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:~/project/" . Then , I logout and login back , open the terminal as su and run the command to execute my script on that folder but the result is command not found.

Then, I change the path in /etc/environment to PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/r0xx4nne/project/" and voila it works.Now i can run the executable script inside ~/project/ without fail under su command.

My question is , what's the difference between ~/project and /home/r0xx4nne/project when it comes in case of creating a path in /etc/environment ?

Why it happened to be like this? I am a newbie and I just want to know more . Thanks for any reply .

1
  • I edited the question a few minutes ago, but it did not change the main objective of the question.Just deleted the sudo bash because it is not necessary to put that here.
    – r0xx4nne
    Jun 18, 2012 at 10:41

3 Answers 3

5

In the shell, ~/project/ is expanded to /home/yourusername/project in most circumstances. This is called tilde expansion.

If you put

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:~/project/

(note, without the quotes) in ~/.profile. Your PATH will be set correctly, because ~/.profile is interpreted by a shell when you log in. /etc/environment (and it's user-specific ~/.pam_environment) is not interpreted by a shell. It is read by the pam_env module during login, but it only accepts NAME=VALUE pairs and no expansions (like $var or ~/ or $(command) etc.) will be done on the VALUE.

1
  • i think this explain everything.. thanks to you,because all i wanted to know is about the expansions you are talking about and the reason why we can't use tilde on /etc/environment . Thumbs up! :D
    – r0xx4nne
    Jun 18, 2012 at 5:27
1

When you login as su, ~ is /root, not /home/<yourusername>. Otherwise, both are the same.

NB: The tilde is expanded by the shell (and not ls) before actually executing the command. For example, if you run sudo ls ~, the command that will be executed is ls /home/<yourusername> and not ls /root. To prevent the shell from expanding the tilde as non-root user, you could:

  • first login as root, then execute the command in the root shell or
  • pass the command as argument to a shell, and make the shell run as root:

    sudo bash -c "ls ~"
    
2
  • 3
    I don't think that's true. If I do sudo ls ~/Desktop, for example, I see my desktop, not root's. Jun 18, 2012 at 4:41
  • i did like @Thanh said , i login as su and do ls ~/Desktop and nothing listed in the terminal. But, it won't work for sudo like @Marty said,because it shows my file on desktop . Thanks to both of you.. now I can see the difference.
    – r0xx4nne
    Jun 18, 2012 at 4:58
0

The tilde stands for the current user's home directory. It may be yours, or it may be another, depending on when it's used. If you are not yet logged in, it won't work.

A related problem is that /etc is not your directory, and is not the place to add your personal paths. If there were another user, why would he have a path to your home directory?

The correct place to modify the path depends on who needs that path:

One user only (you) -- $HOME/.profile, where $HOME is /home/username

All users except root -- /etc/profile

root -- /root/.profile

So, to summarize, your path should be set in /home/r0xx4nne/.profile.

4
  • 1
    ~/.bash_profile is not read during graphical login.
    – geirha
    Jun 18, 2012 at 5:05
  • @geirha: I guess I usually use bash for checking or setting the path, so I once spent time trying to get that working correctly for all types of shells. But I used to run X from a shell, so I suppose it was different. So, what about .profile? I actually see that this is where I set my path. If that's a better place, I can edit my answer. But /etc doesn't seem to be the right place in this case, does it? Jun 18, 2012 at 5:18
  • 1
    Yes, an sh script is run when you log in. It specifically sources ~/.profile before running the gnome-session (or whichever session you chose), so any environment variables set in ~/.profile will stick.
    – geirha
    Jun 18, 2012 at 5:24
  • @geirha - Thanks for the heads-up; I edited my answer, and updated my notes about where to change the path. Somehow, I had researched it a ways back, solved my problem, and made notes, but I guess either something changed or my situation was simply not all-inclusive. Jun 18, 2012 at 15:13

You must log in to answer this question.

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