bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: BASH Command substitution


From: Chet Ramey
Subject: Re: BASH Command substitution
Date: Thu, 17 Dec 2009 11:27:06 -0500
User-agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.5) Gecko/20091204 Thunderbird/3.0

On 12/17/09 6:02 AM, Zoltan Mate wrote:
> Dear Chet Ramey,
> 
> I have a conversation on an other bug forum, then have been directed
> to here, I cannot find any documentation, why

You have to read the section on word splitting.  The output of command
substitution is subject to word splitting, unless the substitution is
quoted.

> $ echo $(echo "'alfa  beta'")
> gives 'alfa beta' whith reduced space, instead of the result of the
> following more logical ways:
> 
> $ echo $(echo "'alfa  beta'")
> the second term suggests one parameter being substituted as
> "<the output of the command>"

It helps to think about the output of each step in the word expansion
process and how that feeds the next step.

The command executed in a subshell is

        echo "'alpha  beta'"

The output is

        'alpha  beta'<newline>

which is read by the calling shell as the results of the command
substitution.  The command substitution code removes the trailing newline
and passes the 'alpha  beta' to the word splitting step.

Since that word is not quoted, it is subject to word splitting.  The
splitting results in two words: "'alpha" and "beta'".  Those two words
are passed to echo as separate arguments.  echo outputs its arguments
separated by spaces and ends with a newline.

There is a program in the bash distribution named `recho' that makes it
clearer.  When I run

recho $(echo "'alpha  beta'")

I get the following:

argv[1] = <'alpha>
argv[2] = <beta'>

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]