LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] Parameter expansion in the shell

[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
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell