Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 "$*" ; }

share|improve this question
up vote 10 down vote accepted

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.