how can i make bash expand whatever it finds in a variable i pass to mkdir?
so far i've tried using eval
and bash -c
, but nothing seems to work
LEVEL_1=1,2,3
LEVEL_2=a,b,c
DATA_L1="/tmp/{$LEVEL_1}"
DATA_L2="$DATA_L1/{$LEVEL_2}"
for LINE in $(cat file.txt) ; do
#"cat" here returns values like
#$DATA_L2/yy/data
mkdir -pv $LINE #it actually contains e.g. this $DATA_L2/yy/data
done
i would expect that this will expand to
mkdir -p /tmp/1/a/yy/data
mkdir -p /tmp/2/a/yy/data
mkdir -p /tmp/3/a/yy/data
mkdir -p /tmp/1/b/yy/data
mkdir -p /tmp/2/b/yy/data
mkdir -p /tmp/3/b/yy/data
mkdir -p /tmp/1/c/yy/data
mkdir -p /tmp/2/c/yy/data
mkdir -p /tmp/3/c/yy/data
I suspect your problem is that you have two layers of expansion, even though you don't show that in your question. That is, you show:
mkdir -pv $DATA_L2/yy/data
And this would work if you simply added an eval
to it:
eval mkdir -pv $DATA_L2/yy/data
But inside your loop, you're not actually running the above command. I think you're running something like:
mkdir -pv $LINE
If we stick an echo
in front of that mkdir
, we can see that:
echo mkdir -pv $LINE
Results in:
mkdir -pv $DATA_L2/yy/data
And:
eval echo mkdir -pv $LINE
Results in:
mkdir -pv /tmp/{1,2,3}/{a,b,c}/yy/data
If you double the eval:
eval eval echo mkdir -pv $LINE
You get what you are looking for:
mkdir -pv /tmp/1/a/yy/data /tmp/1/b/yy/data /tmp/1/c/yy/data /tmp/2/a/yy/data /tmp/2/b/yy/data /tmp/2/c/yy/data /tmp/3/a/yy/data /tmp/3/b/yy/data /tmp/3/c/yy/data
But a double eval
is a really good sign that need to rethink your approach to the problem.