How can I write space in Terminal on OS X?

114,124

Solution 1

The reason it does not work is because the shell splits arguments on whitespace and therefore thinks that you call cd with the two arguments October and Fall. Escape the whitespace with \ like this:

cd October\ Fall

Another way is putting it in single or double quotes like this

cd 'October Fall'
cd "October Fall"

Or just drag and drop the folder into the Terminal.

But why don't you just type a few characters and press Tab for auto-completion?

Solution 2

The shell performs word splitting on arguments before passing it them to the command you specify. This means it takes the line (or, in some cases, lines) and splits it apart into several words, by default at spaces, tabs, and newlines.

As Lưu Vĩnh Phúc mentioned, backslashes and single quotes can be used to prevent word splitting on single characters or everything between them, respectively. Double quotes also work:

cd "October Fall"

In this particular case, ' and " behave identically, but they do have differences.

' disables all special character interpretation (even \), but cannot contain a '. " only disables word splitting and file name expansion (file name expansion is things like ls *.mp3 to list all files with names ending in .mp3. The * triggers the file name expansion here). Since " allows some argument expansions, it's useful if, for example, you have a file name with a space in it inside a variable:

$ filename=October\ Fall
$ #               ^-- Still need a backslash or quoting to prevent word 
$ #                   splitting here, but the backslash/quotes will not be stored
$ cd $filename
bash: cd: October: No such file or directory
$ cd '$filename'
bash: cd: $filename: No such file or directory
$ cd "$filename"
$ pwd
/path/to/October Fall

Note that ' and " do not (by default) cause word splitting themselves, so this, despite being messy, would work for you:

cd "Oct"'ober F'"all"

This is useful if you've got something such as a directory named Octobers' $HOME. The ' in it would break a single pair of ' quoting, and inside " quotes, $HOME will expand to your home directory. But any of these combinations (and many others) will work safely:

cd Octobers\'\ \$HOME
cd "Octobers' "'$HOME'
cd "Octobers' "\$HOME
cd "Octobers' \$HOME"

Something else to keep in mind is that many modern shells offer tab completion. Both Bash and Zsh (probably Ksh as well, but I haven't tested it) can expand part of a directory's name to its full name, complete with safe escaping via \s. So cd Octobers<tab> becomes cd Octobers\'\ \$HOME. You may have to press tab multiple times if there are multiple matches. Also, your shell may not have tab completion enabled by default, check your shell's documentation.

Share:
114,124

Related videos on Youtube

Alexander Khitev
Author by

Alexander Khitev

Updated on September 18, 2022

Comments

  • Alexander Khitev
    Alexander Khitev over 1 year

    I have a folder name with a space between words. I tried to use cd to move into that folder but it didn't work. How can I write space in Terminal on OS X? I tried following

    MacBook-Pro:TestDrop Alexsander$ cd October Fall
    -bash: cd: October: No such file or directory
    MacBook-Pro:TestDrop Alexsander$ cd OctoberFall
    -bash: cd: OctoberFall: No such file or directory
    MacBook-Pro:TestDrop Alexsander$ cd October/ Fall
    -bash: cd: October/: No such file or directory
    MacBook-Pro:TestDrop Alexsander$ cd October/Fall
    -bash: cd: October/Fall: No such file or directory
    MacBook-Pro:TestDrop Alexsander$ cd October-Fall
    -bash: cd: October-Fall: No such file or directory
    MacBook-Pro:TestDrop Alexsander$ cd October_Fall
    
    • Kyslik
      Kyslik over 8 years
      Perhaps loop thru with tabulator key ;)
    • Bakuriu
      Bakuriu over 8 years
      cd October<TAB>.
  • phuclv
    phuclv over 8 years
    you should use backslash, not slash
  • Alexander Khitev
    Alexander Khitev over 8 years
    Yes, I understood you. I could make it.
  • Josiah
    Josiah over 8 years
    TMI but still good to know. +1
  • Blacklight Shining
    Blacklight Shining over 8 years
    @Josiah Explaining why the asker gets the result that they do in addition to how to get the result they want isn't too much information.