0

This question already has an answer here:

I am having issues creating dirs using a variable in a script. Ex:

read user
mkdir $user{01..10}

Interestingly, it will work like this:

read user
mkdir {01..10}$user

Any way to get around this, or what am I missing?

Thanks

marked as duplicate by don_crissti, Stephen Kitt, Community Mar 2 '17 at 19:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

Brace expansion happens before variable expansion in bash. This means that the command

mkdir $user{01..10}

is first expanded to

mkdir $user01 $user02 (etc.)

These variables does not exist, so the final command that is run will be

mkdir

with no operands.

To fix this, use

mkdir "$user"{01..10}
0

You just need to use brackets make the right variable expansion and environment variable resolving:

read user
mkdir ${user}{01..10}

Double quotes although isn't the most elegant way, still works:

read user
mkdir "$user"{01..10}

Not the answer you're looking for? Browse other questions tagged or ask your own question.