help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] How to understand the blank space here


From: lina
Subject: Re: [Help-bash] How to understand the blank space here
Date: Wed, 28 Dec 2011 14:59:51 +0800
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111114 Icedove/3.1.16

On Tuesday 27,December,2011 11:34 PM, Davide Brini wrote:
On Mon, 26 Dec 2011 23:02:53 +0800, lina<address@hidden>  wrote:

Hi,

$ echo \"{Those, words, are, quoted}\"
"{Those, words, are, quoted}"

I have a space after ,
Here bash sees four tokens after echo:

"{Those,

words,

are,

quoted}"

The double quote is part of the first and last argument, because you escaped
it with backslash. Those four tokens become arguments to echo, which
outputs them with a space in between them. Just as if you did

echo a b c d

you'd get

a b c d

as output.

echo \"{Those,words,are,quoted}\"
"Those" "words" "are" "quoted"

Just wonder why the space there changed things,
Here you're seeing a different thing. This is brace expansion. First of
all, since there are no spaces, you're now creating a single token:

"{Those,words,are,quoted}"

The way brace expansion works is that each comma-separated word is
expanded to a separate argument; however, the brace expansion includes a
preamble (a double quote) and a postscript (again a single quote), so the
preamble and the postscript are prepended/appended to each of the generated
arguments, which is what echo finally sees. You can probably see it better
if you do this:

$ echo preamble_{a,b,c,d}_postscript
preamble_a_postscript preamble_b_postscript preamble_c_postscript 
preamble_d_postscript

The only difference is that in your case both the preamble and the
postscript are " (double quote).

Thanks, very subtle design.
I am kinda of understanding now.

Best regards,



reply via email to

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