[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Parameter expansion problem involving ${*#*--}
From: |
Chet Ramey |
Subject: |
Re: Parameter expansion problem involving ${*#*--} |
Date: |
Tue, 3 Oct 2000 11:31:04 -0400 |
> Machine Type: i386-redhat-linux-gnu
>
> Bash Version: 2.04
> Patch Level: 11
> Release Status: release
>
> Description:
> Parameter expansion including pattern matching misbehaves in an
> admittedly obscure case. More precisely, the expansion of
>
> ${*#*--}
>
> returns the wrong value for certain values of $* containing more than
> one occurrence of the substring `--'. The problem seems to be
> confined to the special variable $* and does not occur with "normal"
> (alphanumeric) variables.
No, bash is behaving properly. You have encountered the difference between
unquoted and quoted expansions of $*.
> Repeat-By:
> The problem can be reproduced using the following shell script:
>
> #!/bin/bash
>
> echo 'Value of ${*}: ' ${*}
> echo 'Value of ${*#*--}: ' ${*#*--}
> echo 'Assigning $* to $bla'
> bla="$*"
> echo 'Value of ${bla}: ' ${bla}
> echo 'Value of ${bla#*--}:' ${bla#*--}
>
> Assuming that the script is stored in an executable file called
> `bash-bug', upon execution of
>
> % bash-bug -- --delete
>
> the output is
>
> Value of ${*}: -- --delete
${*} is a list, not a single word.
> Value of ${*#*--}: delete
The manual page says, in the description of the ${parameter#word} expansion:
If parameter is @ or *, the pattern removal operation is applied
to each positional parameter in turn, and the expansion is the
resultant list.
The first word is `--', and is removed completely. The second word is
--delete, and is converted to delete. This is the output you get.
> Assigning $* to $bla
> Value of ${bla}: -- --delete
By using the double quotes, you just converted a list into a string.
(Actually, it would have behaved the same without the double quotes,
since variable assignment doesn't perform word splitting and you
haven't changed $IFS.)
> Value of ${bla#*--}: --delete
This is an operation on a single string.
> Note the difference in the expansion of ${*#*--} and ${bla#*--}.
As the man page documents.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
( ``Discere est Dolere'' -- chet)
Chet Ramey, CWRU chet@po.CWRU.Edu http://cnswww.cns.cwru.edu/~chet/