-1

I am new to the console, and attempting to write a function that will create a directory with the name of the directory being the function argument. This is my function so far:

clidir() {
    mkdir $1
}

Whenever I enter an argument with a space, it creates two directories. I have tried:

clidir "New Folder"

and

clidir New\ Folder

and they both create multiple directories.

Any help is welcome.

Mitchbart
  • 3
  • 2

1 Answers1

2

Double-quote your argument to avoid word-splitting by shell

clidir() {
    mkdir "$1"
}

An excerpt from man bash page,

Word Splitting

The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting. The shell treats each character of IFS as a delimiter, and splits the results of the other expansions into words using these characters as field terminators. If IFS is unset, or its value is exactly , the default, then sequences of , , and at the beginning and end of the results of the previous expansions are ignored, and any sequence of IFS characters not at the beginning or end serves to delimit words.

Community
  • 1
  • 1
Inian
  • 80,270
  • 14
  • 142
  • 161