ssh ufk@  "bash -lc 'pm2 list | grep app | awk { print $3} '"

I need to run the command I pasted here. the problem is that i'm already escaping twice... the bash with " and the pm2 with '. how can i escape awk ?

i know that it will work if i escape it with awk '{ print $3 }' but the all command is already escaped twice... so .. what do i do ?


i created the following bash command:

PM2_APP_ID=`$REMOTE_SRV_SSH_COMMAND "bash -lc \"pm2 list | grep app | grep -v 'pm2 show' | awk '{ print \\\$4 }'\""`;

here i get the all pm2 list relevant line, it's like awk is never been executed.

  • 1
    awk can do most cases of grep and you don't really need those done remotely, so ssh u@h "bash -lc 'pm2 list' " | awk '/app/{print $3}' will also work – dave_thompson_085 Aug 4 '15 at 19:48
  • thanks @dave_thompson_085, you helped me resolve the issue – ufk Aug 23 '15 at 9:13

You can't backslash-escape single quotes inside single-quoted strings, but you can backslash-escape double quotes inside a double-quoted string. So you could do this:

ssh ufk@  "bash -lc \"pm2 list | grep app | awk '{ print \\\$3 }'\""

You need to escape the $ in the awk program twice to avoid the $3 from being expanded, first by local shell and then by the remote one.

  • trying to test it.. i still get the all line from 'grep app', it's like awk is not being called at all – ufk Aug 5 '15 at 6:41
  • @ufk: Sorry. Edited the answer to include necessary backslashes – rici Aug 5 '15 at 7:06
  • updated main post. still not working. thanks for help so far – ufk Aug 23 '15 at 9:10

thanks @dave_thompson_085 for your comment, that helped me resolve the issue.

i'm using the following command:

PM2_APP_ID=`$REMOTE_SRV_SSH_COMMAND "bash -lc 'pm2 list'" | grep app | grep -v 'pm2 show' | awk '{ print $4 }'`;

as you can see here i use pm2 list on remove server, and the rest i'm doing locally. no hassle with escaping things and it works properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.