Re: [ILUG] YABB (Yet Another Bash Bug) - or is it a feature ?

From: Brian Foster (blf at domain utvinternet.ie)
Date: Tue 30 Apr 2002 - 10:11:44 IST


  | Date: Tue, 30 Apr 2002 02:41:31 +0100
  | From: Niall O Broin <niall at domain linux.ie>
  |
  | Take the trivial bash script shown which I'll call echo.sh
  |
  | #!/bin/bash
  | echo $1
  |
  | and pass it an argument with spaces in e.g.
  |
  | echo.sh "a b c d"
  |
  | and the output will be
  |
  | a b c d
  |
  | i.e. all the repeated spaces are compressed to one space.
  | [ needs spaces preserved ... ]

 you'll get lots of replies to this...!

 answer . . . quote it:

                 echo "$1"

 rationale . . in this context, the quotes inform the shell the
               substitution is remain a single word, rather than
               being broken up into its constituent words; i.e.,
               this is a feature of all Bourne-ish shells, not a
               bug per se.

 warning . . . depending on too many conditions (see bash(1)),
               echo 'foo\nbar' may produce either:

                 foo\nbar
               or:
                 foo
                 bar

               which can be, and has been, used to crack systems.

 trivia note . actually, it's the first character of IFS which is
               used to separate constituent words, so, with some
               limitations, a completely off-the-wall answer is:

                 old=$IFS
                 IFS="" # i.e., nothing (empty)
                 echo $1
                 IFS=$old # restore previous IFS

               it is left as an exercise to the reader why I do
               not need to quote the right-hand-side of any of
               the assignments made above (the one time I did
               redundantly use quotes was merely for emphasis).

cheers!
        -blf-

--
 Innovative, very experienced, Unix and      | Brian Foster    Dublin, Ireland
 Chorus (embedded RTOS) kernel internals     | e-mail: blf at domain utvinternet.ie
 expert looking for a new position ...       | mobile: (+353 or 0)86 854 9268
  For a resume, contact me, or see my website  http://www.blf.utvinternet.ie


This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:16:28 GMT