gnu.bash.bug
[Top] [All Lists]

Re: how to pass arguments with space inside?

Subject: Re: how to pass arguments with space inside?
From: Eric Blake
Date: Fri, 10 Apr 2009 15:37:45 +0000 UTC
Newsgroups: gnu.bash.bug

Greg Wooledge <wooledg <at> eeg.ccf.org> writes:

> 
> On Fri, Apr 10, 2009 at 12:02:56AM -0700, lehe wrote:
> > The reason why I don't use "$@" is that the arguments to the bash script is
> > not completely those for the executable. Some of them are just arguments
> > only to the bash script. So actually the script is like
> 
> So you need to build up an *array* of your parameters, not a string.
> What you are trying to do does not, and cannot, work.
> You have built up a string.  There is no way to delimit individual
> parameters inside a string other than whitespace, and when you need
> whitespace *inside* one of those parameters, you're stuck.

Not true, if you are careful and use eval.  For example, something like the 
following (lightly tested here, but based roughly on how autoconf does it in 
configure scripts):

set 'two  spaces' '*' "a'b'c'd"

args=
for arg
do
  case $arg in
    *\'*) arg=${arg//\'/"'\\''"} ;;
  esac
  args+=" '$arg'"
done

eval for arg in "$args" \; do echo \"\$arg\"\; done


And if you don't want to rely on the bash-specific ${name//pattern/repl} or +=, 
you can rewrite those with something more portable to other shells.

-- 
Eric Blake






<Prev in Thread] Current Thread [Next in Thread>