Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

I'm trying to run various commands through sudo, where the equal sign (=) is a part of the command. Under certain cases, it seems that sudo confuses that sign for setting and environment variable.

I see this in sudo(8) man:

Environment variables to be set for the command may also be passed on the command line in the form of VAR=value...

This is a problem for me, for example, if I try to run a command like:

sudo -i "cd /tmp; /usr/bin/hadoop fs -D dfs.replication=2 -ls"

It actually goes into a root shell instead of executing the command, since the command is misinterpreted. I see this in sudo.log:

Apr 29 16:11:40 : my_user : TTY=pts/7 ; PWD=/home/my_user ; USER=root ; ENV=cd /tmp; /usr/bin/hadoop fs -D dfs.replication=2 -ls ; COMMAND=/bin/bash

As you can see, the command is actually misinterpreted as trying to set ENV.

If I remove the preceeding cd /tmp; it will work. However, for some of my stuff, I have to run a cd command or something similar first.

This seems to be because of the equal sign which causes sudo to think I'm setting ENV. If I remove the equal sign (i.e. remove -D dfs.replication=2 ), then it works, and logged correctly:

Apr 29 16:08:46 : my_user : TTY=pts/7 ; PWD=/home/my_user ; USER=root ; COMMAND=/bin/tcsh -c cd /tmp; /usr/bin/hadoop fs -ls

So my question is: How do I escape this = character, and/or get sudo read the command as a whole command, instead of thinking an equal sign is setting an environment variable?

Thanks much!

share|improve this question
cd /tmp; sudo blah blah ? – mcalex Apr 30 '14 at 1:28
I'd try the usual double-dash -- to stop parsing command line arguments first: sudo -i -- "cd /tmp; /usr/bin/hadoop fs -D dfs.replication=2 -ls" (I've no system with sudo install at hand so that I could test it myself). If it's working I'm happily write an answer. – mpy Apr 30 '14 at 17:39
mclex -- good idea, but for some commands I need the cd to be in sudo, as non-privileged user won't be able to access the directory otherwise. – redood May 1 '14 at 18:05
mpy -- I haven't tested your idea thoroughly (since I already went with daBeamer's answer below), but with a simple test it seems to also work. Thanks – redood May 1 '14 at 18:07
up vote 0 down vote accepted

Try executing a shell command via sudo rather than passing one directly. For example, do something like:

sudo bash -c "cd /root; ls -al"

Of course, this is a silly example since one could simply execute sudo ls -al /root, but hopefully it gets the point across.

Executing a shell command is much more explicit than passing one to sudo using the -i flag since that will execute the shell assigned to the root user on a given system. Furthermore, any potential issues with login resource files of the root user are avoided.

Bash (or your preferred shell) should parse out the = character properly. I went ahead and tested this on my end for completeness and didn't encounter any issues. If not, however, just escape the = by prefacing it with a \ character.

share|improve this answer
Not sure why I haven't though of it, but it works! Thanks :) – redood May 1 '14 at 18:01

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.