Score:0

how to read an enviroment variable declared in /etc/environment from bash?

mx flag

How do I read or access the TRAVERSE environment variable below?

nicholas@gondor:~$ 
nicholas@gondor:~$ printenv | grep PATH
WINDOWPATH=2
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
nicholas@gondor:~$ 
nicholas@gondor:~$ printenv | grep TRAVERSE
nicholas@gondor:~$ 
nicholas@gondor:~$ cat /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"



TRAVERSE="/home/nicholas/some_directory"
nicholas@gondor:~$ 

I ran source ~/.bashrc without result. I expect that if I were to reboot the variable would be readable, but that seems extreme.

see also:

https://stackoverflow.com/q/39296472/4531180

Gunnar Hjalmarsson avatar
uz flag
Did you relogin after having added the TRAVERSE variable to `/etc/environment`?
Nicholas Saunders avatar
mx flag
no, I was trying to avoid that. I plan to muck around with some vars for a bit. that's the only way @GunnarHjalmarsson to reload or refresh the vars?
Gunnar Hjalmarsson avatar
uz flag
I'd say it's the "right" way. But you can simply do `source /etc/environment` which will update the current bash process with your latest changes. Well, new variables won't be exported to the environment that way, but only available as shell variables.
raj avatar
cn flag
raj
By definition, `/etc/environment` is read at login, by the login process, before shell or GUI is started, and only at that time. So relogin is a proper way to apply changes in this file.
muru avatar
us flag
The full syntax of `/etc/environment` differs a lot from that of shell scripts, so while your particular file could be sourced, not all valid environment files can be.
muru avatar
us flag
If you just want it for your current shell, why not just set and export the variable yourself?
Gunnar Hjalmarsson avatar
uz flag
@muru: Possibly you mix it up with the `~/.pam_environment` syntax.
Score:1
cn flag

If you are using the bash shell

. /etc/environment

without the dot the variables defined in the script file are not passed to your current shell.

For Example:

$ cat b.sh
TRAVERSE="Another SomeThing"
$ echo $TRAVERSE

$ ./b.sh
$ echo $TRAVERSE

$ . ./b.sh
$ echo $TRAVERSE
Another SomeThing
$ 
SEWTGIYWTKHNTDS avatar
cn flag
`source /etc/environment` should work though
Nicholas Saunders avatar
mx flag
yes, `source /etc/environment` works. thanks, it was a bit late at night.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.