Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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 am trying to sudo a command that uses Awk, and it looks like awk works differently inside sh -c.

echo '1 2' | awk '{print $2}'


sh -c "echo '1 2' | awk '{print $2}'"

1 2

Why is this happening?

share|improve this question
up vote 3 down vote accepted

You use double quotes, therefore $2 is evaluated. The inner single quotes don't affect this anymore. If $2 is empty, you are basically calling awk '{print }'. Consequently, you get the whole input line as output.

You could for example escape the $ with a backslash: \$2

share|improve this answer
@Michael: You can see the behavior described by Maxi by using the -x option of the shell to show a trace: sh -xc "echo '1 2' | awk '{print $2}'" – Dennis Williamson Jun 29 '10 at 3:28

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.