[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Simple echo is printing the wrong value
From: |
Eric Blake |
Subject: |
Re: [Help-bash] Simple echo is printing the wrong value |
Date: |
Wed, 04 Apr 2012 09:09:16 -0600 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:11.0) Gecko/20120329 Thunderbird/11.0.1 |
On 04/04/2012 08:54 AM, Bill Gradwohl wrote:
> cd junkdir
> touch '*'
> touch 'X'
>
> func(){
> echo $# "$@"
> }
>
> for file in *; do
> echo ${file}
> func ${file}
> case ${file} in
> The for loop sees an unquoted asterisk so that expands to the files in the
> directory = 'x' 'X'
> The for loop therefore has 2 elements in the list to work on, the first
> being *.
> Bash provides the value of file which is an asterisk to the echo statement.
> The echo sees an unquoted asterisk, so Bash expands the * to '*' and 'X'
> again, and the output confirms that.
> The function call also confirms that. 2 passed parameters.
> The case statement obviously only sees the * as a literal. Why?
Because 'case' statements have a different set of expansion rules. From
'man bash':
... The word is
expanded using tilde expansion, parameter and variable
expanâ
sion, arithmetic substitution, command substitution,
process
substitution and quote removal.
Note that word splitting and pathname expansion are omitted from this
list, which means that unquoted ${file} in the context of the 'word' of
a case statement does not treat the '*' as a glob. This is true even
without a variable expansion:
$ echo * | wc -c
924
$ case * in ?) echo 1 byte;; esac
1 byte
>
> Why do some things get 2 cracks at expansion and others only 1?
There's only pass through the overall expansion routines (unless you use
'eval'), it's just that there are more than one category of expansion,
and which subset of expansion categories are used is dependent on context.
--
Eric Blake address@hidden +1-919-301-3266
Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature
- Re: [Help-bash] Simple echo is printing the wrong value, (continued)
- Re: [Help-bash] Simple echo is printing the wrong value, Bill Gradwohl, 2012/04/04
- Re: [Help-bash] Simple echo is printing the wrong value, Greg Wooledge, 2012/04/04
- Re: [Help-bash] Simple echo is printing the wrong value, Bill Gradwohl, 2012/04/04
- Re: [Help-bash] Simple echo is printing the wrong value, Greg Wooledge, 2012/04/04
- Re: [Help-bash] Simple echo is printing the wrong value, Chet Ramey, 2012/04/04
- Re: [Help-bash] Simple echo is printing the wrong value, Bill Gradwohl, 2012/04/04
- Re: [Help-bash] Simple echo is printing the wrong value, Greg Wooledge, 2012/04/05
- Re: [Help-bash] Simple echo is printing the wrong value, Chet Ramey, 2012/04/05
- Re: [Help-bash] Simple echo is printing the wrong value, Bob Proulx, 2012/04/05
- Re: [Help-bash] Simple echo is printing the wrong value, DJ Mills, 2012/04/06
- Re: [Help-bash] Simple echo is printing the wrong value,
Eric Blake <=