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 .

Braiam
  • 69,112

3 Answers3

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.

geirha
  • 47,279
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 ~"
    
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.

Marty Fried
  • 18,944