2

I have a env variable test defined in the user root. This can i access inside user root using command:

echo $test

Now i switch to another user myuser using su - myuser . Now How to access the test variable from myuser?

Here is an example of what I am trying to do : enter image description here

4

2 Answers 2

2

If you are using GNU bash (as your login shell on Linux) and if you want some environment variable FOO to be set for every user to string value bar you could add in file /etc/bash.bashrc (near the end) a few lines like:

 # to be added in /etc/bash.bashrc
 export FOO=bar

See §6.2 Bash startup files for more.

Of course, you need root permission to edit (once, e.g. with /usr/bin/sudo /usr/bin/emacs /etc/bash.bashrc in some terminal) that /etc/bash.bashrc file.

You could also choose to use other login shells, like zsh or fish. You need to install their package and use chsh(1) to change the /etc/passwd entry (see also passwd(5) and file /etc/shells documented in shells(5)). They have different ways of doing the same.

If the file /root/.bashrc is world readable (that might be a cybersecurity risk) you could use the eval builtin with grep(1) or gawk(1) on it, using command substitution.

Feel free to ask more by email to [email protected] (near Paris in France).

1

Here's a convoluted way:

root_test=$( sudo su -l -c 'printf "%s\n" "$test"' )

or, assuming root's shell is bash:

eval "$(sudo su -l -c 'declare -p test')"

Either way, you need to become root, in root's login environment, to access the variable's value.

2
  • I tried your both commands but none of them is working for me unfortuantely. I have updated the question with screenshot.
    – Anudocs
    Jan 20, 2022 at 8:35
  • Do some investigation in the root account: how is that variable getting set? Where is it defined? Jan 20, 2022 at 13:33

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