5

I have recently discovered the feature shopt -s autocd:

          autocd  If  set,  a command name that is the name
                  of a directory is executed as if it  were
                  the argument to the cd command.  This op‐
                  tion is only used by interactive shells.

At first glance it seems helpful but I am not an expert Bash user and I wonder if it may be a mistake to use it.

Are there any potential dangers to setting shopt -s autocd? I am especially interested in terms of scripting and conflicts with other applications or configurations.

1
  • You might accidently run commands you don't want to run when directories are named like commands you don't know of.
    – pLumo
    Commented Apr 26, 2021 at 14:51

1 Answer 1

10

I'm going to come out and say no, at least no serious caveats. The reason autocd isn't the default isn't because it's dangerous, it's because shells default to their behavior for scripting and autocd isn't really useful in scripts since its sole purpose is to save typing.

The options you set in your interactive configuration (e.g. .bashrc, .zshrc) have no impact on scripts. They won't conflict with any applications.

It's theoretically possible that some other configurations would conflict with autocd, but it's unlikely. At most you might encounter configurations that don't work when you make use of autocd, but even so I can't think of a plausible example offhand.

A command name always takes precedence over a directory name. For example, if you have a directory called ls, then running ls invokes the ls command, not cd ls. So there's no risk of accidentally changing into a directory instead of running a command, unless you mistype the command name and it happens to match the directory name.

There is a risk that you will accidentally run a command instead of changing into a directory if you mistype the directory name and what you type happens to be the name of a command. This is rarely a concern.

For what it's worth, I've been using autocd for ages, and for me it was one the major selling points of zsh back when bash didn't have it.

1
  • If the command name always takes precedence over directory change shortcut, there is no argument for restricting the warning to mistyping. In fact, the probability, that people give their directories speaking names like find, time, scala is much higher, than mistyping directories called fimd, timme or csala. Commented May 26, 2021 at 13:53

You must log in to answer this question.

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