Shell Quoting. Was: Shell tips and tricks

Steven W. Orr steveo at syslang.net
Mon Oct 8 21:58:53 EDT 2007


On Monday, Oct 8th 2007 at 10:12 -0000, quoth Ben Scott:

=>On 10/8/07, Steven W. Orr <steveo at syslang.net> wrote:
=>> echo $foo               # Also none needed if you *know* that foo has
=>>                         # non-null value
=>
=>  Might be worth going into this in a bit more detail:
=>
=>  Shell quoting is mostly about how arguments are passed to
=>programs[1].  By default, the shell splits every command line into
=>arguments at whitespace.  Quoting simply turns what would otherwise be
=>split into multiple arguments into one argument.  The shell removes
=>the quotes before it passes the argument to the next level.
=>
=>  Quoting is only important on shell command lines (i.e., what us
=>humans type).  Once the command line is parsed, it gets turned into an
=>argument list, and is stored as a series of NUL-terminated strings,
=>and spaces don't matter.
=>
=>  Single quotes (') also turn off most of things you can do with
=>"funny characters" (like $), making it useful for when you want a
=>literal string, exactly as typed, without the shell getting in the
=>way.

In the last couple of weeks, I've been writing some fun Makefiles. Of 
course we have make variables which have to be expanded by make before 
they can be given to shell command lines. And then we have shell variables 
in Makefiles which need double dollas. e.g.

foo	:	bar
	baz=44; echo $${baz}

We even have double dollarsigns	to expand to your process-id at runtime 
so they total up as 4 dolla signs.;

foo	:	bar
	tmpfile=/tmp/fido.$$$$; echo rufus > $${tmpfile}


How about this one?

TMPFILE := /tmp/prog.$$$$$$$$
define funrule_tmplt
${1}_fun:	$${${1}_dep_list}
	$${${2}_tmp}=$${${TMPFILE}}; echo Whatsupdoc >> $${${TMPFILE}}
$${${2}_tmp}
endef
followed by
GENLIST := aa bb cc dd ee ff
${foreach prog,${GENLIST},${eval ${call funrule_tmplt,${prog}IN,${this}}}}
${foreach prog,${GENLIST},${eval ${call funrule_tmplt,${prog}OUT,${that}}}}

Sure, it's contrived but hugely useful. :-)

-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net


More information about the gnhlug-discuss mailing list