bug-bash
[Top][All Lists]
Advanced

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

Re: Confused about how bash breaks input into words


From: Chris F.A. Johnson
Subject: Re: Confused about how bash breaks input into words
Date: Tue, 23 Feb 2010 19:11:07 -0500 (EST)
User-agent: Alpine 2.00 (LMD 1167 2008-08-23)

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:
> > 
> > address@hidden ~] $ echo The date is $(date +'%Y-%m-%d')
> > 2010-02-23
> > 
> > Breaking this into words, using the above rule, results in:
> > 
> > -------     -------------
> > echo        word
> > <space>     metacharacter
> > The         word
> > <space>     metacharacter
> > date        word
> > <space>     metacharacter
> > is          word
> > <space>     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                          <http://cfajohnson.com>
   ===================================================================
   Author:
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
   Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)




reply via email to

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