3

I hate when bash gives me "duh!" kind of error:

foo$ ./bar
-bash: ./bar: is a directory

I used to love AmigaOS shell in which "execution" of a directory simply meant setting it as the current directory:

foo$ ./bar
bar$ 

Is it possible in bash? (without infinite number of aliases of course.)

3 Answers 3

2

Bash 4.0 has the autocd option.

From the Official FAQ:

There is a new 'autocd' option that, when enabled, causes bash to attempt to `cd' to a directory name that is supplied as the first word of a simple command.

Also, take a look at the CDPATH environment variable in man bash for any version since at least as early as 2.0. It allows you to shorten cd commands by searching the listed directories for the directory specified in a cd command.

1

Don't know about bash but this is possible in zsh.

# ~/.zshrc
setopt autocd

Edit: you could probably handle this in bash the same way ubuntu catches command not found

    # bashrc

    function command_not_found_handle {
            if [ -d $1]; then
                cd $1
            else
                echo command not found
            fi
    }
2
  • command_not_found_handle doesn't seem to do anything for me [GNU bash, version 3.2.17(1)-release (i386-apple-darwin9.0)] (tried .bashrc and .profile)
    – Kornel
    Aug 6, 2009 at 23:45
  • I had quick google on this after i posted it, but i'm not sure how debian/ubuntu actually make that function work, unless they've modified bash to do it. Aug 7, 2009 at 1:59
0

As documentation for future searches, this is also possible in csh / tcsh, though the name differs:

# ~/.cshrc
set implicitcd

This value may also be set to verbose, which then echoes the cd commands that are invoked in this manner.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .