How to use single quotes inside awk statement that is surrounded by single quotes? How to use single quotes inside awk statement that is surrounded by single quotes? bash bash

How to use single quotes inside awk statement that is surrounded by single quotes?


You can't nest single quotes, but you can end the single quoted string, include an escaped single quote, and then re-enter the quotes. Try this:

rsh fooDNS '    ...    BROADCAST_IP_ADDRESS=$(/usr/sbin/ifconfig $IF_NAME | grep broadcast |  awk '\''{print $6}'\'')    ...'

Nonetheless, this kind of quoting madness gets ugly very quickly. If at all possible, I recommend using scp/rcp/ftp to copy a normal bash script over to the remote and then run that. Failing that, I think you can use a trick like this if you don't need to feed anything to the script's stdin:

cat script_file | rsh fooDNS bash

(Use rsh fooDNS /bin/sh if your script is plain-sh-compatible and the remote side doesn't have bash, of course.)

As yet another alternative, if your snippet is short you can use here docs:

rsh fooDNS sh <<'EOF'    ...    BROADCAST_IP_ADDRESS=$(/usr/sbin/ifconfig $IF_NAME | grep broadcast |  awk '{print $6}')    ...EOF


Replace the embedded single quotes by the sequence '\'' each time. In theory, you can just do:

rsh fooDNS '    ...    BROADCAST_IP_ADDRESS=$(/usr/sbin/ifconfig $IF_NAME | grep broadcast |    awk '\''{print $6}'\'')    ...'

The first ' ends the current single quoted string; the \' sequence adds a single quote; the final ' restarts a new (but contiguous) single-quoted string. So, this introduces no spaces or anything.

On the other hand, it is best to avoid needing to do that. It is vulnerable to causing problems when the string is reinterpreted. And this is doubly the case when dealing with remote shells.


I usually use double quotes for deep nesting when doing it manually, or a mix of double and single quotes automatically.

If you do it manually, it's probably simpler to just escape your nested double quotes, apostrophes and dollar signs with backslashes, and avoid doing more than one or two levels deep.

Sometimes, instead of nesting the quotes manually, I'll use: http://stromberg.dnsalias.org/~strombrg/bashquote.htmlIt can wrap up a string with double quotes automatically, exponentially. It uses a mix of double quotes and single quotes.

EG. from a minimal test of the code:

dstromberg@zareason-limbo6000a ~/src/home-svn/bashquote/trunk $ ./bashquote.pyunquoted: 'This is a test'repetition 0: ''"'"'This is a test'"'"''repetition 1: ''"'"''"'"'"'"'"'"'"'"'This is a test'"'"'"'"'"'"'"'"''"'"''repetition 2: ''"'"''"'"'"'"'"'"'"'"''"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'This is a test'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"''"'"'"'"'"'"'"'"''"'"''Final version: ''"'"''"'"'"'"'"'"'"'"''"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'This is a test'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"''"'"'"'"'"'"'"'"''"'"''''"'"''"'"'"'"'"'"'"'"''"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'This is a test'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"'"''"'"'"'"'"'"'"'"''"'"''

That should be good for hopping through 3 ssh's and executing on the 4th.