Can’t cd into directory with space in its name, even with quotes or escaping

Posted on

QUESTION :

In a bash shell on a MacOS machine, I can see by typing ls that there is a director Application Support:

$ ls
Accounts                FontCollections     PreferencePanes
Application Scripts     Fonts               Preferences
Application Support     GameKit             Printers
Assistant               Google              PubSub
Assistants              Group Containers    Safari
Audio                   IdentityServices    SafariSafeBrowsing
Caches                  Input Methods       Saved Application State
Calendars               Internet Plug-Ins   Screen Savers
CallServices            Keyboard            Services
ColorPickers            Keyboard Layouts    Sharing
Colors                  KeyboardServices    Sounds
Compositions            Keychains           Spelling
Containers              LanguageModeling    Suggestions
Cookies                 LaunchAgents        SyncedPreferences
CoreFollowUp            Library             VirtualBox
Developer               Logs                Voices
Dictionaries            Mail                WebKit
F5Networks              Messages            com.apple.internal.ck
Family                  Metadata            iMovie
Favorites               Mobile Documents
FileProvider            Passes

However, if I trying to cd into this directory using either quotes or escape characters as described in How to cd to a directory that contains a space in its name?, I still get an error:

LM-SJN-21018636:Library kupeek$ cd "Application Support"
-bash: cd: Application: No such file or directory
LM-SJN-21018636:Library kupeek$ cd Application Support
-bash: cd: Application: No such file or directory

It seems like bash is not ‘picking up’ the quotes or escape characters and is instead looking for a directory called “Application”. Any ideas why this is not working?

ANSWER :

Your cd may be a poorly defined function or alias. Run type -a cd to find out.

A possible alias is explained in this other answer. My answer concentrates on shell functions.

For example if the function used

  • command cd $* (command builtin explained here) or
  • command cd $@ or
  • command cd $1

instead of correct

  • command cd "$@" or
  • command cd "$1" (not as good as the above, still not fatal)

and your Bash didn’t throw too many arguments (pre-4.4 version, see this question), the behavior would be exactly as you described because Application Support being a single argument to the function would undergo word splitting inside the function itself, so command cd would see Application as its first argument.

If your cd is indeed a poorly defined function, you should track the place where it is defined and fix it. An ad hoc solution would be to add the command word before cd, like this:

command cd "Application Support"

or

command cd Application Support

(This will also work if alias is the culprit).

Try using more than one set of quotes,

e.g. put the name in single quotes within double quotes:

cd "'Application Support'"

“An argument enclosed in double quotes presents itself as a single word, even if it contains whitespace separators.” Advanced Bash-Scripting Guide

Using two sets of quotes (quotes within quotes) is a viable workaround, assuming that the problem here is that the original cd command has been aliased. The following definition

alias cd='eval command cd'

would reproduce the problem reported here (in pre-4.4 version of Bash, see this answer). It happens that eval parses the command line for the second time and, on the first pass of parsing, one layer of quoting is removed. Using two sets of quotes works because, removed the first set of quotes, eval will pass the argument with the second set of quotes to cd.

If such alias is the case, the best solution is to fix it in the first place.

There could be more blanks or other non-printable chars that you don’t see. Try cd Application*Scrip* or similar.

IF your cd isn’t a weird alias, then I’d just use TAB with bash’s autocomplete (if it’s enabled) to fill a working format for you.

Typing

cd ApTAB

looks like it should autofill to something like

cd Application S

(If you do have a weird cd alias, then appending Kamil’s answer of typing command cd A... should work with TAB’s too)

and then wait for you to type more of the wanted name (followed by anoter TAB to autocomplete the rest). Or just typing TAB once or twice more will display your available options.

(I’d also not use plain ls, but ls -la or maybe ls -1 instead to see only one entry per line, your ls output isn’t clear)

Leave a Reply

Your email address will not be published.