How can I force bash to expand a variable to pass it as an argument? -


i found weird behavior don't know how workaround.

$ var1=*  $ echo $var1 audiobooks downloads desktop (etc.)  $ ls $var1 audiobooks:  downloads: (etc) 

all seems ok. @ declaration, variable gets expanded , else works. see this:

$ var2=~/rpmbuild/{srpms,rpms/*}/enki-*.rpm  $ echo $var2 /home/yajo/rpmbuild/{srpms,rpms/*}/enki-*.rpm  $ ls $var2 ls: no se puede acceder /home/yajo/rpmbuild/{srpms,rpms/*}/enki-*.rpm: no existe el fichero o el directorio  $ ls /home/yajo/rpmbuild/{srpms,rpms/*}/enki-*.rpm /home/yajo/rpmbuild/rpms/noarch/enki-12.10.3-1.fc18.noarch.rpm  /home/yajo/rpmbuild/srpms/enki-12.10.3-1.fc18.src.rpm /home/yajo/rpmbuild/rpms/noarch/enki-12.10.3-1.fc19.noarch.rpm  /home/yajo/rpmbuild/srpms/enki-12.10.3-1.fc19.src.rpm 

this time, @ declaration ~ gets expanded, produces cannot pass argument ls. however, passing same string literally produces expected results.

questions are:

  • why expand , not?
  • how mimic behavior of $var1 $var2?

thanks.

extra notes:

i tried same double , single quotes, same bad results.

the order in shell parses various aspects of command line not obvious, , matters things this.

first, wildcards aren't expanded @ declaration, they're expanded after variable value substituted (note: in these examples i'll pretend have filesystem):

$ var1=* $ echo "$var1"   # double-quotes prevent additional parsing of variable's value * $ echo $var1     # without double-quotes, variable value undergoes wildcard expansion , word splitting audiobooks:  downloads: (etc) 

btw, ~ is expanded @ declaration, confusing things further:

$ var2=~ $ echo "$var2"   # again, double-quotes let me see what's in variable /home/yajo 

the problem ~/rpmbuild/{srpms,rpms/*}/enki-*.rpm1 while shell wildcard expansion (*) on value after substitution, doesn't brace expansion ({srpms,rpms/*}), it's looking directory names braces , commas in name... , not finding any.

the best way handle store file list array; if right, gets expanded @ declaration:

$ var2=(~/rpmbuild/{srpms,rpms/*}/enki-*.rpm) $ echo "${var2[@]}"   # proper way expand array word list /home/yajo/rpmbuild/rpms/noarch/enki-12.10.3-1.fc18.noarch.rpm  etc... 

note arrays bash extension, , not work in plain posix shells. sure start script #!/bin/bash, not #!/bin/sh.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -