I am creating an instance configuration script that sets up a machine.
I am running the script via sudo i.e. sudo run.sh
. Most of the steps require root access but some of the script's steps do no require root access and I prefer running them as the unprivileged user who ran the sudo.
Inside the script running with sudo, I am trying to do
sudo -i -u username sh -c 'echo $MY_ENV'
Since .bashrc
contains export MY_ENV=something
I expect the above command to print "something"
How can I temporarily switch inside the script to the other user to run commands that include the user's shell env ?
su
's option-
. – Cyrus Apr 11 '15 at 08:24sudo
orsu
, I think. Michael - when you callsh
you don't getbashrc
doing anything - even ifsh
is a link tobash
. You can doENV=~/.bashrc sh -c 'cmd'
though, maybe. A possible issue with that though issudo
. You can dosudo env - ENV=~/.bashrc sh -c 'cmd
. Though I'm not positive if it will correctly work with-c
- have to check that. Oh. jasonwryan - maybe it is aboutsudo
after rereading the question. I guess we're talking about.bashrc
influencing the command before it is run, not a shell's init file. – mikeserv Apr 11 '15 at 10:51.bashrc
file – Michael Apr 11 '15 at 19:17su USERNAME -c "bash -i -c 'echo foo'"
– Cyrus Apr 11 '15 at 19:24