In my .bash_profile, I have
cd(){ builtin cd $1 ls -F }
To change cd to cd ls -F. This seems to work for the most part, but when I want to cd to a multi-word directory, it doesn't work. It thinks that the two words are two separate inputs. To try fixing this, I also tried:
cd "word1 word2"
cd "word1\ word2"
dir=$"word1 word2"
cd "$dir"
and none of these have worked. Do I need to modify my cd function? Or am I just overlooking a clever input method?
This does what (I think) you want:
cd() {
builtin cd "${1-$(echo ~)}" && ls -F
}
Note a few things:
The variable is quoted, so that cd 'some dir with spaces'
will work.
There's a &&
between cd
and ls
, so that the latter will not happen if the former fails. (They could just be on different lines, but then ls
will be run even if cd
fails, and cd
's message about failing will be ‘lost’ among ls
's output. (Not sure how just having the commands on the same line was supposed to work.))
$1
defaults to ~
so that just cd
works as expected.