1

I need to make some directory of Author name. It's easy when doing them alone. I can simply

mkdir "Mario Luis Garcia"

This will create one directory named "Mario Luis Garcia"

But when trying do it massively with shell script, I failed.

for i in "Mario Luis Garcia" "etc.";
    do 
        mkdir $i
    done;

This will create 4 separate directories instead of 2. I tried to use {}, ()to enclose the quoted text in. Doesn't work.

How to do that?

Zen
  • 7,537

1 Answers1

8

You need to quote the variable expansion in double quotes:

mkdir "$i"

(Using single quotes '$i' would create a directory named $i)

If the names can start with -, you should also add -- after the last option argument, indicating that all other arguments are not options, which means in this case they are directory names:

mkdir -- "$i"

Or, with option -v (verbose) for example:

mkdir -v -- "$i"

Volker Siegel
  • 17,283
  • Seigel note that -p flag denotes ignore errors and creates dirs as needed. From help: -p, --parents no error if existing, make parent directories as needed – Simply_Me Aug 10 '14 at 05:35
  • @Simply_Me Right, the idea was to have an option in the example to show the order of options before --. Yes, may be confusing, I'll sinplify. – Volker Siegel Aug 10 '14 at 05:38
  • yeah, IDK if OP is aware of it, so just thought of making it crystal clear. Thanks for the rapid reply. – Simply_Me Aug 10 '14 at 05:40