Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

This question already has an answer here:

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?

share|improve this question

marked as duplicate by Gilles Aug 10 '14 at 22:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

up vote 8 down vote accepted

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"

share|improve this answer
    
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 5: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 5: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 5:40

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