mkdir Public/1 2 3 ----- This line creates folder 1 in the Public folder and creates folders named 2 and 3 in my pwd.

mkdir Public/{1..3} ---- This line creates folders 1,2,3 in Public only as expected.

Can someone explain what is happening.

  • I checked.It does not create, but can you explain how does bash interpret this. Why only 1 was created in Public and others in the pwd. – Lucifer G Oct 31 '20 at 14:29

Your first command is this:

mkdir Public/1 2 3

BASH interprets mkdir Public/1 as creating a directory called 1 inside of the Public directory. The others, 2 and 3, don't have a directory prepended to them so BASH interprets it as creating them in the current working directory.

Your second command is this:

mkdir Public/{1..3}

As you have Public/{1..3}, BASH interprets this as using brace expansion to create directories 1, 2, and 3 inside of the Public directory instead of the current working directory as in the first command.

The same would occur if you were to use the same with rmdir or touch instead of mkdir.


In mkdir Public/1 2 3 there are three arguments: Public/1, 2 and 3. The command is equivalent to

mkdir Public/1
mkdir 2
mkdir 3

In mkdir Public/{1..3} the shell expands {1..3} before mkdir runs. But the shell doesn't expand {1..3} alone. Here {1..3} is a part of a bigger "word". Public/{1..3} expands to Public/1 Public/2 Public/3. You can see this with echo:

echo Public/{1..3}

The real command run by the shell was mkdir Public/1 Public/2 Public/3 which is equivalent to

mkdir Public/1
mkdir Public/2
mkdir Public/3
  • Thank you,this solved almost all of my problems. Btw can we use this sort of expansion with copy...like cp file_name /home/karan/Public/{1..5}.......I tried this but didn't work as it is asking to use the -r option but this option is only required for directories whereas I am trying to copy a file into multiple locations. – Lucifer G Oct 31 '20 at 15:17
  • @LuciferG cp takes just one target; it can take multiple sources though. cp a b c copies a and b to c. It doesn't matter if b and c come from a single expansion in the shell; cp is not aware of what the shell did first. My general answer to "how to copy to multiple destinations?" is here. For a single file use tee. – Kamil Maciorowski Oct 31 '20 at 15:25
  • So unlike cp, mkdir can take multiple arguments i.e mulitple paths whereas cp can take a single arguments i.e a singe path? – Lucifer G Oct 31 '20 at 15:48
  • @LuciferG cp accepts multiple operands, but only one of them is the destination. See man 1 cp. – Kamil Maciorowski Oct 31 '20 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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