Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
8  
Does it need to be bash? zsh can do it (autocd) –  Michael Mrozek Nov 22 '10 at 15:33
1  
+1 for mentioning zsh :) –  iamsid Nov 22 '10 at 15:45
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_n Oct 19 '13 at 14:39

2 Answers 2

up vote 21 down vote accepted

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 $
share|improve this answer
2  
My favorite new feature in bash 4 for interactive use. (For scripting, it's **.) Unfortunately, it's not in older versions. –  Gilles Nov 22 '10 at 19:33

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

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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