Skip to main content
Added examples.
Source Link
Deathgrip
  • 2.8k
  • 1
  • 12
  • 17

You could execute it as:

sudo su - ${USER_NAME} -c 'source ~/.bash_rc; echo $PATH'

An example of it running. The user "zippy" has a ~/.bash_rc file. Note this is in addition to the standard ~/.bashrc file. The user executing the command, "bschuck" does not. The ~zippy/.bash_rc file contains:

#!/bin/bash
# .bash_rc
PATH=$PATH:/this/is/a/dummy/path

Executing this command works:

bschuck@valhalla:~$ sudo su - zippy -c 'source ~/.bash_rc; echo $PATH'
/home/zippy/bin:/home/zippy/.local/bin:\
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin:\
/this/is/a/dummy/path

Yet this command does not keep the new PATH sourced from the ~zippy/.bash_rc file:

bschuck@valhalla:~$ sudo -i -u zippy bash -c 'source ~/.bash_rc; echo $PATH'
/home/zippy/bin:/home/zippy/.local/bin:/usr/local/sbin:\
/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:\
/snap/bin

If you need to run multiple commands, it's best to put them in a script as I suggested yesterday in a comment to my answer.

You could execute it as:

sudo su - ${USER_NAME} -c 'source ~/.bash_rc; echo $PATH'

You could execute it as:

sudo su - ${USER_NAME} -c 'source ~/.bash_rc; echo $PATH'

An example of it running. The user "zippy" has a ~/.bash_rc file. Note this is in addition to the standard ~/.bashrc file. The user executing the command, "bschuck" does not. The ~zippy/.bash_rc file contains:

#!/bin/bash
# .bash_rc
PATH=$PATH:/this/is/a/dummy/path

Executing this command works:

bschuck@valhalla:~$ sudo su - zippy -c 'source ~/.bash_rc; echo $PATH'
/home/zippy/bin:/home/zippy/.local/bin:\
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin:\
/this/is/a/dummy/path

Yet this command does not keep the new PATH sourced from the ~zippy/.bash_rc file:

bschuck@valhalla:~$ sudo -i -u zippy bash -c 'source ~/.bash_rc; echo $PATH'
/home/zippy/bin:/home/zippy/.local/bin:/usr/local/sbin:\
/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:\
/snap/bin

If you need to run multiple commands, it's best to put them in a script as I suggested yesterday in a comment to my answer.

Source Link
Deathgrip
  • 2.8k
  • 1
  • 12
  • 17

You could execute it as:

sudo su - ${USER_NAME} -c 'source ~/.bash_rc; echo $PATH'