bash: Word splitting but when?

Mark J. Reed markjreed@gmail.com
Wed Oct 22 14:16:00 GMT 2008


2008/10/22  sbremal
> Hi All,
>
> Trying to get the right form of quoting and command substitution with output containing spaces.
>
> Given the following two lines in a bash script:
>
> x=$(echo '1 2  3   x')
> y="$(echo '1 2  3   x')"

Those are pretty much equivalent, but not because of when splitting is
done.  The only way the literal command

x=1 2  3  x

could be interpreted is as a request to run the command "2" with
parameters "3" and "x", and with the environment variable "$x" set to
"1".  But this is ruled out syntactically before the command
substituton ever takes place, so the result of the command
substitution winds up being treated as if it were in grouping quotes
even though it's not.  Which is a feature, really, since it prevents
unexpected command execution  (e.g.  x=$(echo foo rm -rf .) ).

If you use something other than assignment, you can easily see the
difference between the quoted and unquoted forms:

$ set $(echo '1 2  3   x')
$ echo $#
4
$ echo "$*"
1 2 3 x

$ set "$(echo '1 2  3   x')"
$ echo $#
1
$ echo "$*"
1 2  3   x

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/



More information about the Cygwin mailing list