X-Recipient: archive-cygwin AT delorie DOT com X-Spam-Check-By: sourceware.org Message-ID: From: To: Subject: bash: Word splitting but when? Date: Wed, 22 Oct 2008 13:54:38 +0000 In-Reply-To: <1224680840.20653.ezmlm@cygwin.com> References: <1224680840 DOT 20653 DOT ezmlm AT cygwin DOT com> Content-Type: text/plain; charset="windows-1256" Content-Transfer-Encoding: 8bit MIME-Version: 1.0 X-IsSubscribed: yes Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com 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/