2

When I run

cd My folder

There is an error because I have not protected the string.

bash: cd: My: No such file or directory

But this works fine

cd "My folder"

Is there a way to redefine cd as an alias or a function so that it automatically protects all the following arguments as one single string?

This approach did not work, as I supposed it wouldn't:

cd () { cd "$*" ; }

Strapakowsky
  • 11,914

1 Answers1

10

You can do it, but only if your directory names don't contain several consecutive spaces, and only if they contain no shell special character other than spaces.

cd () { builtin cd "$*"; }

In practice, use completion: type cd My then press Tab. Bash will insert backslashes before special characters.