Trouble with running cd with sudo as another user

cd-commandsudo

I want to run a command on a shell on behalf of another user. The command is as follows:

sudo -u USER cd /home/USER

I get

sudo: cd: command not found

What is the correct syntax for the command to pass to sudo?

Best Answer

cd is a shell builtin. You need to invoke shell for executing shell builtins:

sudo -u USER sh -c 'cd /home/USER'

OR

sudo -u USER bash -c 'cd /home/USER'
Related Question