1

I have the following command

mkdir -p /tmp/{A,B,C,D}

The above command working fine, but not the below as bash considering B,C and D as different commands:

I want something like below:

mkdir -p /tmp/{A,
B,
C,
D}
Maroun
  • 94,125
  • 30
  • 188
  • 241
Balasekhar Nelli
  • 1,177
  • 4
  • 18
  • 30

1 Answers1

3

As noted in comments, you can do this with backslashes before the newlines, but any whitespace will be regarded as significant, and the whole thing will end up rather ugly. As an aesthetically more pleasurable workaround, you can put the directory names in an array.

dirs=(
    a
    b
    c
    d
  )
mkdir -p "${dirs[@]/#//tmp/}"

The construct "${array[@]/#/prefix)" reproduces the values in array with the prefix prefix added before each.

This is relatively obscure, though; unless the real directory names you are creating are very long and cumbersome, simply writing them out in longhand would be my recommendation.

As noted in comments, both arrays and {A,B,C,D} are Bash constructs, so will not be available if you invoke sh instead of bash (even when sh is a symlink to bash; Bash will notice, and enable POSIX mode).

tripleee
  • 175,061
  • 34
  • 275
  • 318