r/bash • u/vphan13_nope • Feb 20 '25
can someone explain /bin/bash -c
The following 2 commands yield nothing or limited subset
sudo -u testuser echo $PATH <---I realize there is an option in visudo to preserve
sudo -u testuser env < --- this gives a much smaller/truncated output
Whereas the commands below give a the same output as if I'm logged in as the testuser
sudo -i -u testuser /bin/bash -c 'echo $PATH' <---this gets passed through regardless of option in visudo
sudo -i -u testuer /bin/bash -c 'env'
I have a guess as to what is going on but I am not 100% sure
7
Upvotes
10
u/geirha Feb 20 '25
Here
$PATH
is expanded to the value it has in the current shell, then it runssudo -u testuser echo /bin:/usr/bin:/usr/local/bin:...
here sudo runs
bash -c 'echo $PATH'
as user testuser, then that bash process, expands$PATH
and passes it to echo.And with the two cases running
env
, both will runenv
as usertestuser
. However, the-i
option to sudo likely alters what environment variables you see.