17

Is it possible to make bash change directory in command line simply by typing that directory without any commands like cd?

For example is it possible instead of writing this:

$ cd /tmp

I just want to write only this:

$ /tmp

In other words if I call directory as an application then I want to set that directory as a working one.

  • 9
    Does it need to be bash? zsh can do it (autocd) – Michael Mrozek Nov 22 '10 at 15:33
  • 1
    No-no-no, I need solution in bash =) That's the main requirement as in some scientific institutions in the cold-cold Russia there are relics with only bash as a scripting language ;) – Vladimir Nov 22 '10 at 16:08
  • The amount of typing saved by not typing "cd" will likely be offset by not having intelligent default autocompletion for paths. Also this method of changing dirs, as often as one does change dirs, is a little scary in that accidentally running a (potentially malicious) program (that you think is a directory) becomes very likely. – michael Oct 19 '13 at 14:39
  • It works for me in ZSH – ishidex2 Feb 23 at 14:20
36

In bash there is also autocd option. You can enable it by using shopt -s autocd:

pbm@tauri ~ $ shopt -s autocd
pbm@tauri ~ $ django # Now just type this
cd ./django    <- it's done automatically
pbm@tauri ~/django $
  • 2
    My favorite new feature in bash 4 for interactive use. (For scripting, it's **.) Unfortunately, it's not in older versions. – Gilles 'SO- stop being evil' Nov 22 '10 at 19:33
0

I would imagine so. There is some catch-all mechanism that Ubuntu has. When you run a command that isn't on the system, it sometimes says :

No command 'foo' found did you mean:...

I'd bet you can hijack whatever that is and add a check to see if it's given a directory. It's probably worth posting another question asking what does that.

EDIT: Found it: https://wiki.ubuntu.com/CommandNotFoundMagic

  • Yes, that's what one would naïvely expect - however, bash is smarter than us and doesn't even call command_not_found_handle for directories :( – Thomas Themel Nov 22 '10 at 16:23
  • After some research I've found a small patch ( mail-archive.com/bug-bash@gnu.org/msg06761.html ) that could fix it. Unfortunately this patch is unofficial so it could not be used with a stock version of bash provided with ubuntu. – Vladimir Nov 22 '10 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.