Shell tricks - Programming On Unix

Users browsing this thread: 3 Guest(s)
josuah
Long time nixers
How to quote

(skip this if you are used to ' and ")

POSIX sh and derivates (about all of them (but hey! rc's great!)) may be tricky with quoting.

The double quotes (") have the most complex syntax:
- \n get converted to n
- \\ get converted to \
- $(command) and `command` get converted to the result of command
- $var get converted to the value of var

So if we want exactly "$( to be printed, with double quotes, the whole thing would be:

Code:
printf '%s\n' "\"\$("

This is cumbersome in some cases, and very useful in other cases.

The single quotes (') are the strongests: nothing can escape the quotes!

Code:
$ printf '%s\n' 'this\n is " str\\ong!'
this\n is " str\\ong!

These are convenient for printf format strings (as above), sed or awk scripts... in which we want the \n not to be transformed into n by the shell, but interpreted by printf, awk...


Messages In This Thread
Shell tricks - by josuah - 13-11-2017, 05:27 AM
RE: Shell tricks - by josuah - 13-11-2017, 05:39 AM
RE: Shell tricks - by venam - 14-11-2017, 01:53 AM
RE: Shell tricks - by josuah - 23-11-2017, 07:59 AM
RE: Shell tricks - by josuah - 23-11-2017, 08:52 AM
RE: Shell tricks - by josuah - 04-12-2017, 07:43 AM
RE: Shell tricks - by z3bra - 04-12-2017, 08:11 PM
RE: Shell tricks - by josuah - 05-12-2017, 08:07 AM
RE: Shell tricks - by josuah - 05-12-2017, 09:02 AM
RE: Shell tricks - by josuah - 05-12-2017, 09:08 AM
RE: Shell tricks - by josuah - 08-12-2017, 12:25 PM