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'
Share:
85,416

Related videos on Youtube

chhaya vishwakarma
Author by

chhaya vishwakarma

Updated on September 18, 2022

Comments

  • chhaya vishwakarma
    chhaya vishwakarma almost 2 years

    I'm trying to create a directory which has space in name e.g. "user test" when I fire mkdir -p "user test" it works for me.

    When I put "user test" in variable mkdir fails, it creates "user and test" separately

    var="user test"
    mkdir -p $var 
    

    I also tried mkdir -p "$var"

    Can someone please suggest where I'm doing wrong ?

    • Stéphane Chazelas
      Stéphane Chazelas over 8 years
      What happens when you do mkdir -p "$var" (which should be the correct syntax for POSIX shells or even better mkdir -p -- "$var", see unix.stackexchange.com/q/131766)?
    • Stéphane Chazelas
      Stéphane Chazelas over 8 years
      What's the shell? In rc, it should be var='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
      Basile Starynkevitch over 8 years
      Don't create directory with spaces in their name. Use an underscore mkdir user_test. It will be much easier after.
    • Abel Cheung
      Abel Cheung over 8 years
      As 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--
      ott-- over 8 years
      On what system and with which shell have you tried this? The normal mkdir -p $var should create 2 directories user and test, while mkdir -p "$var" should create only 1 dir user test.
  • woozymj
    woozymj over 8 years
    It is, I just wanted to provide more details. I have acknowledged ZN13's response in my answer now.
  • Cloud Cho
    Cloud Cho over 2 years
    Would you be specific at "It is never recommended to use space"? Is this due to the difficulty in search?