www.delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2008/10/22/10:16:50

X-Recipient: archive-cygwin AT delorie DOT com
X-Spam-Check-By: sourceware.org
Message-ID: <f60fe000810220714m7bfd868cq8a9588038f817f20@mail.gmail.com>
Date: Wed, 22 Oct 2008 10:14:42 -0400
From: "Mark J. Reed" <markjreed AT gmail DOT com>
To: cygwin AT cygwin DOT com
Subject: Re: bash: Word splitting but when?
In-Reply-To: <BAY119-W554558D88293827703F914A9290@phx.gbl>
MIME-Version: 1.0
References: <1224680840 DOT 20653 DOT ezmlm AT cygwin DOT com> <BAY119-W554558D88293827703F914A9290 AT phx DOT gbl>
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019