4

In a bash, is there a way to combine autocd and CDPATH, that is, to go to a subdirectory of a CDPATH element without typing cd before the dir name?

For example, if i have these directories:

  • ~
  • ~/Leute
  • ~/Leute/Hinz
  • ~/Leute/Kunz
  • ~/Dinge
  • ~/Dinge/Haus
  • ~/Dinge/Auto

and do

~$ shopt -s autocd
~$ export CDPATH=":~/Leute:~/Dinge"

then i can do

~$ Leute

and end up in the ~/Leute directory (that is the autocd thing)

~/Leute$

and i can do

~/Leute$ cd Auto

and end up in ~/Dinge/Auto (the CDPATH thing)

~/Dinge/Auto$

but combining the two seems not to work. For example starting in ~/Leute i can’t jump to ~/Dinge/Auto without an explicit cd:

~/Leute$ Auto
bash: Auto: Command not found...

Is there a reason for this? Or is there something else needed to make this work?

3

Whether by design or oversight, it seems that the test for whether you've typed a directory name doesn't search the CDPATH variable:

execute.cmd.c, function execute_simple_command:

  if (autocd && interactive && words->word && is_dirname (words->word->word))
    {
      words = make_word_list (make_word ("cd"), words);
      xtrace_print_word_list (words, 0);
      goto run_builtin;
    }

The definition of is_dirname is in the same source file:

/* Return 1 if the file found by searching $PATH for PATHNAME, defaulting
   to PATHNAME, is a directory.  Used by the autocd code below. */
static int
is_dirname (pathname)
     char *pathname;
{
  char *temp;
  int ret;

  temp = search_for_command (pathname, 0);
  ret = (temp ? file_isdir (temp) : file_isdir (pathname));
  free (temp);
  return ret;
}

From this, it appears that autocd will find directories on PATH, but I tested it with X11 (for /usr/bin/X11/) and got the error X11: No such file or directory.

|improve this answer|||||

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.