Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is this normal?

> sudo -u misc -i "ls"
note test tmp
> sudo -u misc -i "ls ~"
-bash: ls ~: command not found
> sudo -u misc -i "ls ~"
-bash: ls ~: command not found
> sudo -u misc -i "foo=bar"

Anything that's got a space in it seems to be interpreted as a single command, which is unrecognised. Even weirder, setting any environment variable drops me into the other user's account. How can I run those commands as expected?

share|improve this question
up vote 4 down vote accepted

The command and arguments passed to sudo should not be surrounded in quotes, so you can try

sudo -u misc -i ls -l


sudo -u misc -i ls ~

should not list misc's home directory content, but your home, because the ~ character is expanded before been passed to sudo. You could try to avoid this using

sudo -u misc -i ls '~'

but this again doesn't work, because sudo do not invoke a shell to execute its command, so ~ has no meaning out of a shell.

Finally, the solution could be

sudo -u misc -i bash -c 'ls ~'
share|improve this answer
I never realised how complicated it is to do something this simple with sudo :). But this works and also works nicely setting environment variables. Thanks. – mahemoff Jan 4 '12 at 18:56
Just revisited this and found some gotchas, workarounds explained in this thread. sudo -u misc -i bash -c 'cd /tmp ; pwd' doesn't work for example. Need sudo -u misc -i bash '-c "cd /tmp"'. But a better way is to pipe it into bash ( echo 'cd /tmp; pwd' | sudo -u misc -i bash – mahemoff Jan 20 '12 at 21:07
@mahemoff: in those two threads there seem to be no reference to sudo, and the command sudo -u user -i bash -c 'cd tmp; pwd' works perfectly here. – enzotib Jan 20 '12 at 21:24
No, not sudo, but similar idea. (I was searching for bash pwd as i figured others had similar issues.) I just tried now on a standard 10.04 build and pwd didn't show /tmp, it showed the home directory. – mahemoff Jan 20 '12 at 23:47

It works for me but without the quotations marks:

sudo -u test -i ls -lah ~

share|improve this answer
It lists your home, not that of testuser. – enzotib Jan 3 '12 at 22:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.