bash: Word splitting but when?

Morche Matthias Matthias.Morche@P7S1Produktion.de
Wed Oct 22 14:25:00 GMT 2008


Not cygwin related.


-----Original Message-----
From: cygwin-owner@cygwin.com [mailto:cygwin-owner@cygwin.com] On Behalf Of sbremal@hotmail.com
Sent: Wednesday, October 22, 2008 3:55 PM
To: cygwin@cygwin.com
Subject: bash: Word splitting but when?


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')"

According to bash manual:

"The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting.

the ouput of $(echo '1 2  3   x') should go through word splitting and x should have the value "1 2 3 x". While y should have "1 2  3   x". This does not seem to be the case, see below some tests.

Do you have any idea how the manual shall be interpreted?

Cheers,
Balazs

---

$ cat z
x=$(echo '1 2  3   x')
y="$(echo '1 2  3   x')"
echo "$x"
echo "$y"

$ bash -x z
++ echo '1 2  3   x'
+ x='1 2  3   x'
++ echo '1 2  3   x'
+ y='1 2  3   x'
+ echo '1 2  3   x'
1 2  3   x
+ echo '1 2  3   x'
1 2  3   x

---

Relevant sections from the bash manual:

EXPANSION
       Expansion is performed on the command line after it has been split into words.  There are seven kinds of expan-
       sion performed: brace expansion, tilde expansion,  parameter  and  variable  expansion,  command  substitution,
       arithmetic expansion, word splitting, and pathname expansion.

       The  order of expansions is: brace expansion, tilde expansion, parameter, variable and arithmetic expansion and
       command substitution (done in a left-to-right fashion), word splitting, and pathname expansion.

       On systems that can support it, there is an additional expansion available: process substitution.

       Only brace expansion, word splitting, and pathname expansion can change the number of words of  the  expansion;
       other expansions expand a single word to a single word.  The only exceptions to this are the expansions of "$@"
       and "${name[@]}" as explained above (see PARAMETERS).

...

   Command Substitution
       Command substitution allows the output of a command to replace the command name.  There are two forms:

              $(command)
       or
              `command`

       Bash performs the expansion by executing command and replacing the command substitution with the standard  out-
       put  of  the  command,  with any trailing newlines deleted.  Embedded newlines are not deleted, but they may be
       removed during word splitting.  The command substitution $(cat file) can be  replaced  by  the  equivalent  but
       faster $(< file).

       When  the  old-style  backquote form of substitution is used, backslash retains its literal meaning except when
       followed by $, `, or \.  The first backquote not preceded by a backslash terminates the  command  substitution.
       When  using  the  $(command) form, all characters between the parentheses make up the command; none are treated
       specially.

       Command substitutions may be nested.  To nest when using the backquoted form, escape the inner backquotes  with
       backslashes.

       If  the  substitution  appears within double quotes, word splitting and pathname expansion are not performed on
       the results.

...

   Word Splitting
       The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not
       occur within double quotes for word splitting.

       The  shell  treats  each  character  of IFS as a delimiter, and splits the results of the other expansions into
       words on these characters.  If IFS is unset, or its value is exactly , the  default,  then
       any  sequence  of  IFS  characters  serves  to  delimit words.  If IFS has a value other than the default, then
       sequences of the whitespace characters space and tab are ignored at the beginning and end of the word, as  long
       as the whitespace character is in the value of IFS (an IFS whitespace character).  Any character in IFS that is
       not IFS whitespace, along with any adjacent IFS whitespace characters, delimits a field.   A  sequence  of  IFS
       whitespace characters is also treated as a delimiter.  If the value of IFS is null, no word splitting occurs.

       Explicit  null  arguments ("" or '') are retained.  Unquoted implicit null arguments, resulting from the expan-
       sion of parameters that have no values, are removed.  If a parameter with no value is  expanded  within  double
       quotes, a null argument results and is retained.

       Note that if no expansion occurs, no splitting is performed.

_________________________________________________________________
Connect to the next generation of MSN Messenger 
http://imagine-msn.com/messenger/launch80/default.aspx?locale=en-us&source=wlmailtagline

--
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/


--
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