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: Eric Blake
Subject: Re: Confused about how bash breaks input into words
Date: Tue, 23 Feb 2010 17:03:52 -0700
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666

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:
> 
> [me@host ~] $ 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.

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

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

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