Re: Problem with pattern replacing when STRING is an expandable char
Mike Stroyan wrote: 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]/*}" *** $ Thanks a lot for your fast response! Ok, even after so many years bash is astounding if you don't have all expansion rules in mind every time. Kind regards, Ado
Re: Problem with pattern replacing when STRING is an expandable char
Paul Jarc wrote: Heinz-Ado Arnolds <[EMAIL PROTECTED]> wrote: 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. No, it's expanded afterward, because the variable expansion isn't quoted. This does what you want: echo "${a//[0-9]/*}" 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. 3.1.17 behaves the same way as 3.2.25. You see a different result because of a different set of files between the two situations, not because of the different bash version. If there are no files in the current directory that match ***.*, then pathname expansion will leave it unchanged. paul Thanks a lot for your fast response! Ok, even after so many years bash is astounding if you don't have all expansion rules in mind every time. Kind regards, Ado
Re: Problem with pattern replacing when STRING is an expandable char
> > 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 <[EMAIL PROTECTED]>
Re: Problem with pattern replacing when STRING is an expandable char
Heinz-Ado Arnolds <[EMAIL PROTECTED]> writes: > 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. Only those that contain a period. > Seems that the "*" > is expanded before replacing the pattern. No, it is expanded afterwards, since filename expansion is carried out after parameter expansion. You need to quote it properly. Andreas. -- Andreas Schwab, SuSE Labs, [EMAIL PROTECTED] SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany PGP key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different."
Re: Problem with pattern replacing when STRING is an expandable char
Heinz-Ado Arnolds <[EMAIL PROTECTED]> wrote: > 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. No, it's expanded afterward, because the variable expansion isn't quoted. This does what you want: echo "${a//[0-9]/*}" > 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. 3.1.17 behaves the same way as 3.2.25. You see a different result because of a different set of files between the two situations, not because of the different bash version. If there are no files in the current directory that match ***.*, then pathname expansion will leave it unchanged. paul
Problem with pattern replacing when STRING is an expandable char
Configuration Information [Automatically generated, do not change]: Machine: i686 OS: linux-gnu Compiler: gcc Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='i686' -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i686-pc-linux-gnu' -DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H -I. -I/tmp/S/bash-3.2 -I/tmp/S/bash-3.2/include -I/tmp/S/bash-3.2/lib -g -O2 uname output: Linux * 2.6.23.1 #1 SMP PREEMPT Tue Oct 16 16:47:14 CEST 2007 i686 GNU/Linux Machine Type: i686-pc-linux-gnu Bash Version: 3.2 Patch Level: 25 Release Status: release Description: Pattern replacing doesn't work like documented with special characters "*" and "?" as replacment strings 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. Kind regards, H.-A. Arnolds -- Dipl.-Ing. Heinz-Ado Arnolds MPI fuer Astrophysik Karl-Schwarzschild-Strasse 1 D-85748 Garching Phone: +49/89/3-2217 FAX : +49/89/3-2388 email: arnolds[at]MPA-Garching.MPG.DE