[ILUG] Passing arguments containing spaces

Brian Foster blf at blf.utvinternet.ie
Fri Dec 30 18:04:23 GMT 2005


  | Date: Fri, 30 Dec 2005 14:39:25 +0000
  | From: Liam O'Hagan <liam at liamohagan.com>
  | 
  |[ ... ] if I do this :
  | command=3D"mp3info -a \"Louis Armstrong\" -l \"The Essential Louis\" -t \
  |   \"Basin Street Blues\" \"Basin Street Blues.mp3\""
  | 
  | Then issue :
  | $command
  | 
  | [ it obviously won't work ].

 as others have pointed out, for what yer doing,
 you should consider rewriting it as command='...'
 (mostly for clarity) and then, in any case, use eval.

 the problem here is since eval effectively re-parses
 its arguments, if you have a shell metacharacter such
 as $ or ? or * or ` or so on as part of the value of
 $command, you may run into additional problems.

 there is _another_ way of doing essentially what
 yer doing that does not have that glitch:

    declare -a command=( mp3info -a "Louis Armstrong" ... )
    "${command[@]}"

 i.e., use an array.  no need for strange quoting,
 no need for an eval, and no problems with embedded
 $, ?, *, and so on.  the above syntax is, AFAIK,
 GNU bash(1)-only, albeit there is an equivalent
 in the Korn shell (ksh(1)).
 ( for various other Bourne-ish shells without
  arrays, there is a kludge or two which is
  equivalent to the above. )

cheers!
	-blf-
-- 
Experienced (20+ yrs) kernel/software Eng: | Brian Foster   Montpellier,
 • Unix, embedded, &tc;  • Linux;  • doc;  | blf at utvinternet.ie   FRANCE
 • IDL, automated testing, process, &tc.   |  Stop E$$o (ExxonMobile)!
Résumé (CV) http://www.blf.utvinternet.ie  |     http://www.stopesso.com



More information about the ILUG mailing list