Super User is a question and answer site for computer enthusiasts and power users. 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 have this command line that I enter into terminal and it works as intended:

dscl . -readall /Users UniqueID | awk '/^RecordName:/ {name=$2}; /^UniqueID: / {if ($2 > 500) print name}'

What I want to do is use sh -c "insert command string here" and when I try and use the above statement, it gives me these errors:

awk: syntax error at source line 1
 context is
    /^RecordName:/ >>>  {name=} <<< 
awk: illegal statement at source line 1
awk: illegal statement at source line 1

Any idea how I would correct this? I need it for a program in objective-c.

share|improve this question
up vote 4 down vote accepted

Single quotes don't prevent variable expansion inside double quotes:

$ echo "a'$RANDOM'"

You could replace $ with \$ or ' with '\'':

$ sh -c "echo a b | awk '{print \$2}'"
$ sh -c 'echo a b | awk '\''{print $2}'\'

Or use a heredoc:

sh -s <<'END'
echo a b | awk '{print $2}'
share|improve this answer
Perfect, thank you so much. – John Oct 10 '12 at 23:00

Your awk command does not have a closing '.

share|improve this answer
My mistake, I just forgot to add that to the question but had it in my code. – John Oct 10 '12 at 23:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .