Re: Problem with pattern replacing when STRING is an expandable char
From:
Mike Stroyan
Subject:
Re: Problem with pattern replacing when STRING is an expandable char
Date:
Wed, 12 Dec 2007 13:05:07 -0700
User-agent:
Mutt/1.5.15+20070412 (2007-04-11)
>
> Repeat-By:
> a=111.1
> echo ${a//[0-9]/x}
>
> correctly gives "xxx.x", but
>
> echo ${a//[0-9]/*}
>
> gives a listing of files in current directory. Seems that the "*"
> is expanded before replacing the pattern.
>
> It workes the right way at least up to bash-3.1.17(1)-release
>
> But if you set
>
> a=111
>
> it doesn't even work in 3.1.17 right.
The pathname expansion of "*" is not done until after the parameter
expansion substitution. That is the documented behavior. The following
example shows that echo of the "***.*" pattern matches files and
directories that have a "." in their name. Setting a to "111" results
in a pathname pattern of "***" that matches all of the files.
Double quoting the substitution prevents pathname expansion.
$ echo $BASH_VERSION
3.2.25(1)-release
$ touch a b c.d e.f
$ ls
a b c.d e.f
$ a=111.1
$ echo ${a//[0-9]/*}
c.d e.f
$ echo "${a//[0-9]/*}"
***.*
$ a=111
$ echo ${a//[0-9]/*}
a b c.d e.f
$ echo "${a//[0-9]/*}"
***
$
--
Mike Stroyan <mike@stroyan.net>