Bash – How to sudo if in Bash

bashshellsudo

I need to check for a directory in the home directory of another user. Normally I would sudo but that forks another process and I also lose my environment.

For example, I have:

if [[ -d "/home/otheruser/svn" ]];
then
   echo "SVN exists"
else
   echo "SVN does not exist"
fi

I need the the test condition to run with root permissions.

Best Answer

if sudo test -d "/home/otheruser/svn"; then