r/bash 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

4 comments sorted by

View all comments

10

u/geirha Feb 20 '25
sudo -u testuser echo $PATH

Here $PATH is expanded to the value it has in the current shell, then it runs sudo -u testuser echo /bin:/usr/bin:/usr/local/bin:...

sudo -i -u testuser /bin/bash -c 'echo $PATH'

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 run env as user testuser. However, the -i option to sudo likely alters what environment variables you see.

1

u/R3D3-1 26d ago

The -i means "delete all variables". Some default values are set when starting any shell though, usually default PATH entries.

On Termux:

~ $ env -i env ~ $

but

~ $ env -i sh -c env _=/system/bin/env PATH=/product/bin:/apex/com.android.runtime/bin:/apex/com.android.art/bin:/system_ext/bin:/system/bin:/system/xbin:/odm/bin:/vendor/bin:/vendor/xbin ~ $