cd to a directory by typing its name?

For example if a directory 'blob' exists and I just type 'blob[return]' then the system cds into the blob directory for me.

In Linux (Ubuntu) I can add shopt -s autocd to my .bashrc file, but on OS X this gives an error: -bash: shopt: autocd: invalid shell option name


Solution 1:

autocd was added in bash 4.0. You can install a newer version of bash with Homebrew and then change the default login shell with chsh:

brew install bash;echo /usr/local/bin/bash|sudo tee -a /etc/shells;chsh -s /usr/local/bin/bash

After you open a new login shell (or a tab by default in Terminal or iTerm 2), echo $BASH_VERSION should print something like 4.2.45(2)-release.

Terminal and iTerm 2 open new shells as login shells by default, so bash reads ~/.bash_profile but not ~/.bashrc. If you don't source ~/.bashrc from ~/.bash_profile or anything, add shopt -s autocd to ~/.bash_profile instead of ~/.bashrc.

Solution 2:

The output of shopt -p can be of some help here. It prints a list of settable options. Sadly, on Lion, autocd is not one of them (see excerpt below).

Edit Lauri's answer above has a solution for updating your shell to include autocd

$ shopt -p
shopt -u cdable_vars
shopt -u cdspell
shopt -u checkhash
shopt -s checkwinsize
shopt -s cmdhist
shopt -u compat31
shopt -u dotglob
shopt -u execfail
shopt -s expand_aliases
shopt -u extdebug
shopt -u extglob
shopt -s extquote
shopt -u failglob
shopt -s force_fignore
shopt -u gnu_errfmt
shopt -u histappend
shopt -u histreedit
shopt -u histverify
shopt -s hostcomplete
shopt -u huponexit
shopt -s interactive_comments
shopt -u lithist
shopt -s login_shell
shopt -u mailwarn
shopt -u no_empty_cmd_completion
shopt -u nocaseglob
shopt -u nocasematch
shopt -u nullglob
shopt -s progcomp
shopt -s promptvars
shopt -u restricted_shell
shopt -u shift_verbose
shopt -s sourcepath
shopt -u xpg_echo