Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mkdir issue in bash script

Tags:

bash

mkdir

I am trying to create a folder tree using the mkdir command, which is supposed to have the following structure:

rootfs
├── Fol1
│   ├── Fol11
│   └── Fol12
└── Fol2

I sucessfully created this tree using

mkdir -p /rootfs/{Fol1/{Fol11,Fol12},Fol2}

However the folder rootfs is supposed to be variable, which is why I tried

ROOT=/rootfs
FOLDERTREE=/{Fol1/{Fol11,Fol12},Fol2}
mkdir -p "$ROOT$FILETREE"

Although echo "$ROOT$FILETREE" yields exactly /rootfs/{Fol1/{Fol11,Fol12},Fol2} I do get a wrong filetree

rootfs
└── {Fol1
    └── {Fol11,Fol12},Fol2}

What am I doing wrong here ?

like image 823
Flo Ryan Avatar asked Jan 08 '23 16:01

Flo Ryan


2 Answers

Braces are not processed in the result of variable substitution. Use:

mkdir -p "$ROOT"/{Fol1/{Fol11,Fol12},Fol2}
like image 170
Barmar Avatar answered Jan 18 '23 21:01

Barmar


You can use BASH array to keep all the directory paths as:

dirs=( "${ROOT}"/{Fol1/{Fol11,Fol12},Fol2} )

Then create it as:

mkdir -p "${dirs[@]}"
like image 39
anubhava Avatar answered Jan 18 '23 21:01

anubhava