Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Shell parsing is hard.

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.


I find it rather annoying to escape single quotes within single-quoted strings in the shell. Really, there are only two options, both of which are rather verbose:

  $ 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: