Issue
I have a user myuser
and an environment variable test
export var=test
Saved bashprofile and works fine.
When running a shell script, I like to pass above variable and echo it with different user like below.
sudo su - anotheruser -c 'echo ${var}'
I tried this and it did not work. How do I pass the variable in the shell script ?
Thanks in advance.
Solution
Use the -E
flag of sudo
to maintain environment variables:
sudo -E su anotheruser -c 'echo ${var}'
Answered By - Aplet123 Answer Checked By - Senaida (WPSolving Volunteer)