This is the mail archive of the
cygwin
mailing list for the Cygwin project.
Re: bash: Word splitting but when?
- From: "Mark J. Reed" <markjreed at gmail dot com>
- To: cygwin at cygwin dot com
- Date: Wed, 22 Oct 2008 10:14:42 -0400
- Subject: Re: bash: Word splitting but when?
- References: <1224680840.20653.ezmlm@cygwin.com> <BAY119-W554558D88293827703F914A9290@phx.gbl>
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/