Awk doesn't work when inside double quotes
Solution 1:
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}'"