1

I executed the following code in Ubuntu server 16.04 xenial:

mkdir -p /root/backups/{db, dirs}

I recall that in another system, it worked like charm creating all 3 dirs:

/root/backups/
/root/backups/db
/root backup/dirs

Yet this time the result was:

/root/backups/
/root/backups/{db,

Why is this partial, broken result?

1 Answer 1

13

Bash, like any POSIX shell, splits commands into tokens before expanding words (which, in Bash, includes brace expansion).

mkdir -p /root/backups/{db, dirs}

contains a space, so the command is first split into the tokens mkdir, -p, /root/backups/{db,, and dirs}. None of these need further expansion, so mkdir is run with the three arguments -p, /root/backups/{db, and dirs}. It creates {db in /root/backups, and dirs} in the current directory.

If you drop the space you’ll get the behaviour you’re after.

3
  • No way to bypass that "spaceophobia" right there? Commented Jan 9, 2018 at 17:00
  • 1
    No, because you might actually want those three oddly named directories for some reason.
    – DopeGhoti
    Commented Jan 9, 2018 at 17:13
  • 4
    With the space escaped or quoted, it would create /root/backups/db and /root/backups/␣dirs, with the space. Which probably isn't what you want.
    – ilkkachu
    Commented Jan 9, 2018 at 17:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .