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

This command doesn't work:

ssh $HOST "ls -l | awk '{print $1}'"`

Above ignores the command awk. I think it might be because of the double quotes?

Also, how would I add another set of double quotes inside the awk?


ssh $HOST "awk '{print $1 "*"}' /some_file"

I tried escaping the quotes, I even tried this:

ssh $HOST "awk '{print $1 "\""*"\""}' /some_file"

without success.

share|improve this question
up vote 14 down vote accepted

Variable interpolation is performed within double quotes, so here's what I think might be happening: when you type in ssh $HOST "ls -l | awk '{print $1}'", your shell (the one on your local computer, where you are running the SSH client) sees $1 within the double quotes and replaces it with the value of the variable $1, which will be blank. It isn't able to detect that the $1 is nested within single quotes within the double quotes. So what winds up getting sent to the remote server is

ls -l | awk '{print }'

which is basically equivalent to

ls -l | cat

i.e. it just prints out the output of ls -l.

Solution: escape the $ with a backslash,

ssh $HOST "ls -l | awk '{print \$1}'"
share|improve this answer
Haha, that was so simple! I just looked right through it! This did exactly what I wanted. ssh $HOST "ls -l | awk '{print \$1 \"*\"}'" Thanks a bunch! – Nick Aug 20 '10 at 18:28

You must log in to answer this question.

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