How to get another user’s PATH in Bash on Linux?

Posted on In QA

How to get the $PATH environment variables of another user in Bash on Linux? The current user running the Bash script is root.

If the scripts is run by root, this piece of code will store the PATH from the environment of the user user1:

user1path=$(su - $user1 -c env | grep ^PATH= | cut -d'=' -f2-)

It runs env command as the $user to print out its environment and find out the $PATH.

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *