by pgn674 · 5y ago · 0 comments
In general, not for SSH specifically, I tend to close quoted bash strings instead of trying to figure out how many escapes I need. Especially useful for awk and sed.
For example:
$ echo "One Two Three" | awk '{print "'"'"'"$2" here'"'"'"}'
'Two here'
Ok, maybe escapes would have been easier in this particular example. $ echo "One Two Three" | awk '{print "\'"$2" here\'"}'
>
No wait, that doesn't work. Why didn't that work? $ echo "One Two Three" | awk '{print "\\'"$2" here\\'"}'
awk: cmd. line:1: {print "\\
awk: cmd. line:1: ^ unterminated string
awk: cmd. line:1: {print "\\
awk: cmd. line:1: ^ syntax error
...Nevermind, my original way is still better for me. $ echo 'A'\''A, B'"'"'B'
A'A, B'B
I suppose that there's a few different commands that would work for your example: $ echo "One Two Three" | awk '{print "'"'"'"$2" here'"'"'"}' # original
$ echo "One Two Three" | awk {print\ \"\'\"\$2\"\ here\'\"}
$ echo "One Two Three" | awk '{print "'\''"$2" here'\''"}'
$ echo "One Two Three" | awk "{print \"'\"\$2\" here'\"}"
To be fair, though, the argument {print "'"$2" here'"} looks pretty weird if you're not used to working with awk.