I have a variable that is set through .bashrc
.
In ~/.bashrc
:
PROJ_HOME=~/Projects/stable
From a bash shell, I'd like to do something like this:
$ su -l kenneth -c 'echo $PROJ_HOME'
However, when I do this, the expected /home/kenneth/Projects/stable
is not printed out.
Any ideas on how I can do this?
To display your current environment variables, use the env command. An environment variable that is accessible to all your processes is called a global variable.
You can set your own variables at the command line per session, or make them permanent by placing them into the ~/. bashrc file, ~/. profile , or whichever startup file you use for your default shell. On the command line, enter your environment variable and its value as you did earlier when changing the PATH variable.
There are two ways to get around this. The sudo has a handy argument -E or --preserve-env which will pass all your environment variables into the sudo environment. Note, we are using the /usr/bin/env command above, which simply echo's all the environment variables.
Have you tried the option su -m ?
-m, --preserve-environment
do not reset environment variables
For example: su -m kenneth -c 'echo $PROJ_HOME'
You need to export the variable. You may not need to use the -m
option to su
to preserve the environment.
export PROJ_HOME=~/Projects/stable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With