Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

1 Answer 1

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

 
discard

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.