This question already has an answer here:

I'm trying to make a bash script that would make a series of directories and requesting a parameter of how many directories should be created.

$> ./createDir.sh 5

$> ls

ex_01
ex_02
ex_03
ex_04
ex_05

I tried using mkdir ex_{01..$1} but it does not seem correct. How could I make this work (without using any loop)?

marked as duplicate by don_crissti, Stéphane Chazelas bash Oct 4 '16 at 13:07

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.

up vote 1 down vote accepted

You will need eval for this.

#!/bin/bash

start=1
stop=$1

mkdir $(eval echo ex_{$start..$stop})

But I agree with don_crissti, why not simply use a loop?

Before:

ls -p | grep 'ex_'
<empty>

After I run the script:

./makeDirs.sh 3
ls -p | grep 'ex_'
ex_1/
ex_2/
ex_3/

Further reading:

  • Good on including the "why is eval bad" link. – Wildcard Oct 4 '16 at 12:57
  • 2
    No need for echo. eval "mkdir ex_{$start..$stop}" should be enough. – Stéphane Chazelas Oct 4 '16 at 13:04
  • @StéphaneChazelas Yes, you're right. Works without echo as well. – maulinglawns Oct 4 '16 at 15:35

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