5

So I started with this discussion, and it basically does exactly what I want.

However, the solution to preserve directory whitespace using quotes fails when using arguments, e.g. cd -P dir. Any ideas for preserving whitespace and allowing for option flags?

This is the function I'm currently using.

function cd {
    new_directory="$*";
    if [ $# -eq 0 ]; then 
        new_directory=${HOME};
    fi;
    builtin cd "${new_directory}" && ls;
}

For example, with the above, cd -P My\ Documents/ becomes cd "-P My Documents" which obviously fails.

Jon
  • 148

1 Answers1

5

When you write $*, you've lost: this erases the distinction between spaces in an argument and spaces to separate arguments. Use "$@" instead, which expands to one word per argument.

function cd {
  builtin cd "$@" && ls
}