19

I am a happy BASH user. I do not want to switch to another shell (in this case ZSH).

ZSH has this ability to change a directory without necessarily typing:

cd /to/a/directory

What would be the correct alias (or maybe BASH function) to change directories without having to type cd?

On my above example, moving to /to/a/directory would be done like this:

/to/a/directory

I have tried:

alias ''='cd '
alias ""='cd '
alias " "='cd '

Any ideas?

Sildoreth
  • 1,883
  • 1
  • 25
  • 38
alfredodeza
  • 5,058
  • 4
  • 35
  • 44
  • 9
    If an empty alias were allowed as in your first two attempts, and if it worked the way you intended, then ALL commands would turn into "cd" commands. – Rob Kennedy May 20 '09 at 21:24

2 Answers2

32

It's an option added in version 4.0 of Bash. You can set it with:

$ shopt -s autocd

Put that in your .bashrc file to enable it always.

dbr
  • 165,801
  • 69
  • 278
  • 343
mipadi
  • 398,885
  • 90
  • 523
  • 479
  • Thanks, it seems I will have to wait on this one: bash: shopt: autocd: invalid shell option name – alfredodeza May 20 '09 at 21:21
  • I added the version information to the question, and linked to the changelog (it's under section 3 "New Features in Bash", item d) – dbr May 21 '09 at 03:32
  • @alfredodeza Well, you could do `shopt -s autocd 2> /dev/null`. Then you can have the satisfaction of having the command there without the annoyance of the error message. :) – Sildoreth Mar 12 '15 at 03:25
6

I don't think you'll be able to have a "blank" alias, it doesn't really make sense..

bash version 4.0 added the autocd option, which is set by running shopt -s autocd (as mipadi answered)

There is no such option in previous versions of bash, annoying.

dbr
  • 165,801
  • 69
  • 278
  • 343
  • autocd aside, might have been able to get this by using traps, which could get ugly, but i think it would work to some degree... – osirisgothra Feb 16 '14 at 14:35