Re: Confused about how bash breaks input into words

2010-02-24 Thread Marc Herbert
Eric Blake a écrit :
> 
> Another good reference is POSIX:
> http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_03

A less rigorous and easier reading is the Guide to Unix Shell Quoting:









Re: Confused about how bash breaks input into words

2010-02-23 Thread DennisW
On Feb 23, 8:41 pm, Allen Halsey  wrote:
> Eric Blake  redhat.com> writes:
>
>
>
> > But you missed that:
>
> > $(date +'%Y-%m-%d')
>
> > is an entire word (basically, an unquoted $ character consumes until the
> > end of the shell substitution, command substitution, or arithmetic
> > substitution, and that entire scan becomes part of the current word being
> > parsed).
>
> Thank you Eric and Chris.
>
> The link to the POSIX section on Token Recognition really helped my
> understanding.
>
> I don't think I missed anything in the Bash Reference Manual that
> explains this.
>
> Allen

>From the Bash info-file:

"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 filename
expansion."

So command substitution is performed before word splitting.


Re: Confused about how bash breaks input into words

2010-02-23 Thread Allen Halsey
Eric Blake  redhat.com> writes:
>
> But you missed that:
> 
> $(date +'%Y-%m-%d')
> 
> is an entire word (basically, an unquoted $ character consumes until the
> end of the shell substitution, command substitution, or arithmetic
> substitution, and that entire scan becomes part of the current word being
> parsed).
> 

Thank you Eric and Chris.

The link to the POSIX section on Token Recognition really helped my 
understanding.

I don't think I missed anything in the Bash Reference Manual that 
explains this.

Allen






Re: Confused about how bash breaks input into words

2010-02-23 Thread Chris F.A. Johnson
On Tue, 23 Feb 2010, Eric Blake wrote:

> According to Allen Halsey on 2/23/2010 3:50 PM:
> > Hi,
> > 
> > I'm trying to understand bash's parsing model.
> > 
> > I read in the manual that the shell "breaks the input into words and 
> > operators,
> > obeying the quoting rules described in Quoting. These tokens are separated 
> > by
> > metacharacters."
> 
> Another good reference is POSIX:
> http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_03
> 
> > Taking this simple example:
> > 
> > [...@host ~] $ echo The date is $(date +'%Y-%m-%d')
> > 2010-02-23
> > 
> > Breaking this into words, using the above rule, results in:
> > 
> > --- -
> > echoword
> >  metacharacter
> > The word
> >  metacharacter
> > dateword
> >  metacharacter
> > is  word
> >  metacharacter
> 
> So far, so good.  But you missed that:
> 
> $(date +'%Y-%m-%d')
> 
> is an entire word (basically, an unquoted $ character consumes until the
> end of the shell substitution, command substitution, or arithmetic
> substitution, and that entire scan becomes part of the current word being
> parsed).
> 
> > This confuses me because, intuitively, I feel that the command substitution,
> > $(date +'%Y-%m-%d'), should be treated as a single word.
> 
> Yes, it is a single word.

But it is subject to wordsplitting:

$ print "%s\n" $(date +'%Y %m %d')
2010
02
23


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




Re: Confused about how bash breaks input into words

2010-02-23 Thread Eric Blake
According to Allen Halsey on 2/23/2010 3:50 PM:
> Hi,
> 
> I'm trying to understand bash's parsing model.
> 
> I read in the manual that the shell "breaks the input into words and 
> operators,
> obeying the quoting rules described in Quoting. These tokens are separated by
> metacharacters."

Another good reference is POSIX:
http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_03

> Taking this simple example:
> 
> [...@host ~] $ echo The date is $(date +'%Y-%m-%d')
> 2010-02-23
> 
> Breaking this into words, using the above rule, results in:
> 
> --- -
> echoword
>  metacharacter
> The word
>  metacharacter
> dateword
>  metacharacter
> is  word
>  metacharacter

So far, so good.  But you missed that:

$(date +'%Y-%m-%d')

is an entire word (basically, an unquoted $ character consumes until the
end of the shell substitution, command substitution, or arithmetic
substitution, and that entire scan becomes part of the current word being
parsed).

> This confuses me because, intuitively, I feel that the command substitution,
> $(date +'%Y-%m-%d'), should be treated as a single word.

Yes, it is a single word.

-- 
Eric Blake   ebl...@redhat.com+1-801-349-2682
Libvirt virtualization library http://libvirt.org



signature.asc
Description: OpenPGP digital signature


Confused about how bash breaks input into words

2010-02-23 Thread Allen Halsey
Hi,

I'm trying to understand bash's parsing model.

I read in the manual that the shell "breaks the input into words and operators,
obeying the quoting rules described in Quoting. These tokens are separated by
metacharacters."

Taking this simple example:

[...@host ~] $ echo The date is $(date +'%Y-%m-%d')
2010-02-23

Breaking this into words, using the above rule, results in:

--- -
echoword
 metacharacter
The word
 metacharacter
dateword
 metacharacter
is  word
 metacharacter
$   word
(   metacharacter
dateword
 metacharacter
+'%Y-%m-%d' word
)   metacharacter

This confuses me because, intuitively, I feel that the command substitution,
$(date +'%Y-%m-%d'), should be treated as a single word.

Indeed, the manual later says "The words that are not variable assignments or
redirections are expanded (see Shell Expansions)." This suggests to me that a
command substitution, indeed all expansions, should be treated as a single word.

Can someone elucidate how bash breaks the input into words?

Thank you,

Allen Halsey