Creating directory with space in name
85,416
You have to escape the space
mkdir hello\ there
You can also encapsulate the string, this way you do not have to escape the space.
mkdir 'hello there'
Related videos on Youtube
Author by
chhaya vishwakarma
Updated on September 18, 2022Comments
-
chhaya vishwakarma almost 2 years
I'm trying to create a directory which has space in name e.g.
"user test"
when I firemkdir -p "user test"
it works for me.When I put
"user test"
in variable mkdir fails, it creates"user
andtest"
separatelyvar="user test" mkdir -p $var
I also tried
mkdir -p "$var"
Can someone please suggest where I'm doing wrong ?
-
Stéphane Chazelas over 8 yearsWhat happens when you do
mkdir -p "$var"
(which should be the correct syntax for POSIX shells or even bettermkdir -p -- "$var"
, see unix.stackexchange.com/q/131766)? -
Stéphane Chazelas over 8 yearsWhat's the shell? In
rc
, it should bevar='user test'; mkdir -p -- $var
as double quotes are not special in that shell and there's no implicit split+glob on unquoted variables like there is in Bourne-like shells. -
Basile Starynkevitch over 8 yearsDon't create directory with spaces in their name. Use an underscore
mkdir user_test
. It will be much easier after. -
Abel Cheung over 8 yearsAs a side note, I find certain attitude in answer and comment worrisome. Such "wisdom" exists because of sloppy scripts and programs written during earlier days. Instead of solving the artificial restriction, such attitude encourages the sloppy behavior to persist and spread. In the long run, one would need more time to troubleshoot why something doesn't work, or even causing trouble for others.
-
ott-- over 8 yearsOn what system and with which shell have you tried this? The normal
mkdir -p $var
should create 2 directoriesuser
andtest
, whilemkdir -p "$var"
should create only 1 diruser test
.
-
-
woozymj over 8 yearsIt is, I just wanted to provide more details. I have acknowledged ZN13's response in my answer now.
-
Cloud Cho over 2 yearsWould you be specific at "It is never recommended to use space"? Is this due to the difficulty in search?