[ILUG] Parameter expansion in the shell

Brian Foster blf at utvinternet.ie
Thu Jan 17 19:16:42 GMT 2008


  | Date: Thu, 17 Jan 2008 11:02:49 -0000 (UTC)
  | From: "Nick Murtagh" <nickm at go2.ie>
  | 
  | >  some people advocate always using ${foo}, claiming
  | >  it improves clarify --- Clearly Wrong since the OP
  | >  was(? is?) confused --- and/or prevents mistakes,
  | >  which whilst broadly true, also means the script's
  | >  author does not understand the shell or its syntax
  | >  (since, in practice, the problem rarely arises and
  | >  some(? many?) people find excessive un-necessary
  | >  use of the construct annoying).
  |[ ... ]
  | Personally I would agree with "some people" because I think
  | having two ways to do the same thing - one of which breaks in
  | certain circumstances - sucks. Why not just have one which
  | works reliably?

 Ok, in C, eliminate ++ and --, ->, unary + and -,
 [], += &tc, and various other redundant (and, as
 it also happens, often easier to type AND read)
 operators.  and there's also a few redundant
 keywords as well ....

 in EREs, eliminate ?, +, *, and so on; again,
 there is a more powerful, harder to type AND
 read, alternative.

 we don't need all these tools with `grep'-like
 functionality; eliminate all but one.

 each syntax has its place, and if used properly
 isn't a problem.  $foo is perfectly reliable
 when used properly.  ${foo} will go wrong when
 not used properly.  for instance:

    echo 'foo is ${foo}.'

 very probably is not what is intended.  ($foo
 in that example is equivalent and also does not
 do what you probably meant.)

 an example from the Plan 9 `rc' shell might be
 interesting here.  (I may have the details below
 slightly messed up, but the basics are, AFAICR,
 correct.)  originally in `rc' you _had_ to use
 the concatenation operator ^ so:

   to do ...    you HAD to write ...
    ${foo}bar    $foo^bar
    $foo$bar     $foo^$bar
    foo$bar      foo^$bar

 and so on.  solves the problem in what seems to
 be a rather neat manner.  but the user community
 did not agree, and complained about being forced
 to use ^ when it was obvious what was meant.

 this is significant:  at the time, `rc' was a
 brand-new shell, so the backwards-compatibility
 issue did not exist.  (`rc's syntax is notably
 different from Bourne's, so all but very simple
 Unix scripts had to be re-written for Plan 9.)

 when yer users are people like bwk and dmr, you
 listen!  ;-)   hence, "implicit concatenation"
 was added.  nowadays the ^ operator is needed
 only in the first case (when what is meant is
 not obvious, similar to Bourne-ish shells).

 for small scripts, always using ${foo} may be Ok.
 for large scripts — here I'm speaking as someone
 who has written more than one interpreter in
 Bourne-ish shell (multi-thousand line scripts) —
 there's no way in hades I'll use ${foo} all the
 time.  most of the time it is not needed and is
 a nuisance.  (like ^ in Plan 9 `rc'?)

cheers!
	-blf-
-- 
“How many surrealists does it take to    |  Brian Foster
 change a lightbulb?  Three.  One calms  |  somewhere in south of France
 the warthog, and two fill the bathtub   |     Stop E$$o (ExxonMobile)!
 with brightly-coloured machine tools.”  |       http://www.stopesso.com



More information about the ILUG mailing list