I have a script:
#!/bin/bash
SINGLE_FILE=/tmp/blah.file
MULTIPLE_FILES=/tmp/{dir1,dir2}/*.file
cp $SINGLE_FILE $MULTIPLE_FILES /tmp/newDir
This will fail with:
cp: cannot stat `/tmp/{dir1,dir2}/*.file': No such file or directory
It looks like the script is not expanding out the wildcards in my variables. So I can make this script work with:
eval cp $SINGLE_FILE $MULTIPLE_FILES /tmp/newDir
My question is: Why is the script not expanding my variable in the first place? Also, is there a different way to solve this other than using eval
?
The script is expanding your variable, to the value you set it to, i.e.
/tmp/{dir1,dir2}/*.file
, right?And as you have rightly discovered, you then need to ask the shell to 'run thru that line again' and re evaluate any variables you find there.
So, no other way besides eval (well you could subshell stuff and waste a lot of processing power, and essentially remake the feature of eval with your own code.)
The only thing I can recommend is to use the shell debugging feature
set -vx
to see for yourself how it is working, i.e.