20

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.

Renan
  • 17,136
Vladimir
  • 310
  • 11
    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 '19 at 14:20

2 Answers2

37

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 $
pbm
  • 25,387
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

Shawn J. Goff
  • 46,081
  • 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 ( http://www.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