0

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

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}
| improve this answer | |
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}
| improve this answer | |

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