Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

BASH Command substitution

8 views
Skip to first unread message

Zoltan Mate

unread,
Dec 17, 2009, 6:02:35 AM12/17/09
to ch...@po.cwru.edu, bug-...@gnu.org
Dear Chet Ramey,

I have a conversation on an other bug forum, then have been directed
to here, I cannot find any documentation, why

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

On the other hand, having made the substitution, one should get the
$ echo 'alfa beta'
text to be interpreted.


So why the '-s are quoted and the spaces are not?
This strange behaviour should be mentioned in the bash manual.

Zoltan Mate
----------------------------------------- The former conversation:
--------------------------------------------
bash command substitution reduce double spaces in strings:

$ echo $(echo "'alfa beta'")
'alfa beta'


Reproducible: Always

Steps to Reproduce:


------- Comment #1 From SpanKY 2009-12-16 19:32:24 0000 [reply] -------

the second isnt actually quoted which means you ran:
argv[] = {
"echo"
"'alfa"
"beta'"
}


------- Comment #2 From ZoliM 2009-12-17 09:41:08 0000 [reply] -------

Where is emphasized the text interpreatation process in the manual?

In
$ echo $(echo "'alfa beta'")
the second term suggests one parameter being substituted as
"<the output of the command>"

On the other hand, having made the substitution, one should get the
$ echo 'alfa beta'
text to be interpreted.

So I motion to mark in the documentation at the Command substitution chapter
the proper interpretation logic.


------- Comment #3 From SpanKY 2009-12-17 10:32:32 0000 [reply] -------

those two examples are not equivalent. your first snippet boils down to:
echo \'alfa beta\'

this bugzilla isnt a forum for teaching people how to script bash. if you want
further help, please ask on the bash mailing list, or the gentoo forums, or
some other suitable location.


Chris F.A. Johnson

unread,
Dec 17, 2009, 9:37:11 AM12/17/09
to bug-...@gnu.org
On Thu, 17 Dec 2009, 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
>
> $ 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>"
>
> On the other hand, having made the substitution, one should get the
> $ echo 'alfa beta'
> text to be interpreted.
>
>
> So why the '-s are quoted and the spaces are not?
> This strange behaviour should be mentioned in the bash manual.

The output of the command substitution is two arguments. It you
want it intepreted as one, quote it:

echo "$(echo "'alfa beta'")"

--
Chris F.A. Johnson, webmaster <http://woodbine-gerrard.com>
===================================================================
Author:
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)


Gerard

unread,
Dec 17, 2009, 11:26:18 AM12/17/09
to bug-...@gnu.org
On Thu, 17 Dec 2009 12:02:35 +0100
Zoltan Mate <mat...@gmail.com> articulated:

> this bugzilla isnt a forum for teaching people how to script bash.
> if you want further help, please ask on the bash mailing list, or the
> gentoo forums, or some other suitable location.

Where exactly is the "bash mailing list"?

--
Gerard
ger...@seibercom.net

|::::=======
|::::=======
|===========
|===========
|

Against stupidity the very gods Themselves contend in vain.


Friedrich von Schiller, "The Maid of Orleans", III, 6

Chet Ramey

unread,
Dec 17, 2009, 11:27:06 AM12/17/09
to Zoltan Mate, bug-...@gnu.org, chet....@case.edu
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 ch...@case.edu http://cnswww.cns.cwru.edu/~chet/


0 new messages