0

This question already has an answer here:

I try to create a permanent environment variable for all users of an Ubuntu machine which is simply a path to a specific often used folder.

I don't want to enter the whole path everytime when I cd to that folder.

I already tried some solutions, but none worked for me.


What I've tried

https://unix.stackexchange.com/a/117470/124191

I entered the following line to /etc/environment:

WEB="/opt/lampp/htdocs"

https://unix.stackexchange.com/a/74080/124191

I added the following line to /etc/profile.d/myvariable.sh:

WEB=/opt/lampp/htdocs

I tried all these solutions, and tested them by echoing out the new environment variable like this: echo $WEB in my terminal.

But I get nothing back. What am I doing wrong?

Linux Version:

Linux 4.8.0-45-generic #48~16.04.1-Ubuntu SMP Fri Mar 24 12:46:56 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

UPDATE:


I had to restart my terminal, now If I execute echo $WEB as root, I get:

/opt/lampp/htdocs:/opt/lampp/htdocs/

I thought it is there twice because I have an entry in /etc/environment and one in /etc/profile.d/myvariable.sh.

But I still get /opt/lampp/htdocs:/opt/lampp/htdocs/ even after deleting the entry in /etc/profile.d/myvariable.sh and restarting the terminal.

UPDATE:

I just found the following entry export WEB=${WEB}:/opt/lampp/htdocs/ inside /root/.bash_profile, It was still there because I tried another unmentioned tutorial earlier, which also did not mention that I have to restart the terminal after editing the file, so I thought it did not worked too...

Now it works with root, but not with any other user.

marked as duplicate by ilkkachu, Archemar, countermode, Jeff Schaller, thrig Apr 7 '17 at 15:29

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.

  • Update: I had to restart my terminal, now If I execute echo $WEB as root, I get: /opt/lampp/htdocs:/opt/lampp/htdocs/. I thought it is there twice because I have an entry in /etc/environment and one in /etc/profile.d/myvariable.sh, but I get the same output even after deleting the entry in /etc/profile.d/myvariable.sh and restarting the terminal?! – Black Apr 5 '17 at 12:34
  • I closed this since what you describe is standard behavior. Those files are only read once, when a shell session starts, so any changes you make won't take effect until you start a new session (open a new terminal, for example) – terdon Apr 5 '17 at 12:35
  • But why do I get /opt/lampp/htdocs:/opt/lampp/htdocs/ after executing echo $WEB instead of /opt/lampp/htdocs ? And why does it only "work" with root and not with all users? – Black Apr 5 '17 at 12:36
  • No idea, that's not what your question is asking :). Please edit and add all this to your question. Explain exactly what files you edited and how and exactly how you are testing. What do you mean it only works as root? How are you testing this? All this needs to be in your question so we can understand what the issue is. – terdon Apr 5 '17 at 12:37
  • 1
    @Black, yep, that you do. envvars are inherited to child processes from the parent, but there's no way to change the environment of a process from outside after the process has started. Of course you can assign the variable manually for the current shell on the command line directly, or by asking the shell to read the startup file where the var is defined, say with . /etc/environment. (The command is really just a single dot.) – ilkkachu Apr 5 '17 at 15:25