fuumind
fuumind

Reputation: 127

mkdir multiple subfolders in bash script

I have the following script:

#!/bin/bash
path="/parentfolder/{child_1,child_2}"
mkdir -p $path
mkdir -p /parentfolder/{child_3,child_4}

Running it creates the following folders:

/parentfolder/{child_1,child_2}
/parentfolder/child_3
/parentfolder/child_4

How can I make the script create the following folder structure:

/parentfolder/child_1
/parentfolder/child_2
/parentfolder/child_3
/parentfolder/child_4

Upvotes: 0

Views: 1411

Answers (2)

tripleee
tripleee

Reputation: 189377

You cannot use brace expansion in a quoted variable; either put the braces in the command itself, or assign the variable differently. If you need the values to be in a variable, using an array would seem suitable.

#!/bin/bash

paths=(/parentfolder/{child_1,child_2,child_3,child_4})
mkdir -p "${paths[@]}"

Upvotes: 3

Kessem Lee
Kessem Lee

Reputation: 1373

path=`echo /parentfolder/{child_1,child_2}`

expansion needs a command to work properly.

Upvotes: 0

Related Questions