462fb2
diff --git a/bashline.c b/bashline.c
462fb2
--- a/bashline.c
462fb2
+++ b/bashline.c
462fb2
@@ -117,6 +117,7 @@ static char *restore_tilde __P((char *, char *));
462fb2
 
462fb2
 static char *bash_filename_rewrite_hook __P((char *, int));
462fb2
 static void bash_directory_expansion __P((char **));
462fb2
+static int bash_filename_stat_hook __P((char **));
462fb2
 static int bash_directory_completion_hook __P((char **));
462fb2
 static int filename_completion_ignore __P((char **));
462fb2
 static int bash_push_line __P((void));
462fb2
@@ -1414,7 +1415,7 @@ bash_default_completion (text, start, end, qc, compflags)
462fb2
      const char *text;
462fb2
      int start, end, qc, compflags;
462fb2
 {
462fb2
-  char **matches;
462fb2
+  char **matches, *t;
462fb2
 
462fb2
   matches = (char **)NULL;
462fb2
 
462fb2
@@ -1424,7 +1425,19 @@ bash_default_completion (text, start, end, qc, compflags)
462fb2
       if (qc != '\'' && text[1] == '(') /* ) */
462fb2
 	matches = rl_completion_matches (text, command_subst_completion_function);
462fb2
       else
462fb2
-	matches = rl_completion_matches (text, variable_completion_function);
462fb2
+	{
462fb2
+	  matches = rl_completion_matches (text, variable_completion_function);
462fb2
+	  if (matches && matches[0] && matches[1] == 0)
462fb2
+	    {
462fb2
+	      t = savestring (matches[0]);
462fb2
+	      bash_filename_stat_hook (&t);
462fb2
+	      /* doesn't use test_for_directory because that performs tilde
462fb2
+		 expansion */
462fb2
+	      if (file_isdir (t))
462fb2
+		rl_completion_append_character = '/';
462fb2
+	      free (t);
462fb2
+	    }
462fb2
+	}
462fb2
     }
462fb2
 
462fb2
   /* If the word starts in `~', and there is no slash in the word, then
462fb2
@@ -2763,6 +2776,57 @@ restore_directory_hook (hookf)
462fb2
     rl_directory_rewrite_hook = hookf;
462fb2
 }
462fb2
 
462fb2
+static int
462fb2
+bash_filename_stat_hook (dirname)
462fb2
+     char **dirname;
462fb2
+{
462fb2
+  char *local_dirname, *new_dirname, *t;
462fb2
+  int should_expand_dirname, return_value;
462fb2
+  WORD_LIST *wl;
462fb2
+  struct stat sb;
462fb2
+
462fb2
+  local_dirname = *dirname;
462fb2
+  should_expand_dirname = return_value = 0;
462fb2
+  if (t = mbschr (local_dirname, '$'))
462fb2
+    should_expand_dirname = '$';
462fb2
+  else if (t = mbschr (local_dirname, '`'))	/* XXX */
462fb2
+    should_expand_dirname = '`';
462fb2
+
462fb2
+#if defined (HAVE_LSTAT)
462fb2
+  if (should_expand_dirname && lstat (local_dirname, &sb) == 0)
462fb2
+#else
462fb2
+  if (should_expand_dirname && stat (local_dirname, &sb) == 0)
462fb2
+#endif
462fb2
+    should_expand_dirname = 0;
462fb2
+  
462fb2
+  if (should_expand_dirname)  
462fb2
+    {
462fb2
+      new_dirname = savestring (local_dirname);
462fb2
+      wl = expand_prompt_string (new_dirname, 0, W_NOCOMSUB|W_NOPROCSUB);    /* does the right thing */ 
462fb2
+      if (wl)
462fb2
+	{
462fb2
+	  free (new_dirname);
462fb2
+	  new_dirname = string_list (wl);
462fb2
+	  /* Tell the completer we actually expanded something and change
462fb2
+	     *dirname only if we expanded to something non-null -- stat
462fb2
+	     behaves unpredictably when passed null or empty strings */
462fb2
+	  if (new_dirname && *new_dirname)
462fb2
+	    {
462fb2
+          free (local_dirname); /* XXX */
462fb2
+	      local_dirname = *dirname = new_dirname; 
462fb2
+	      return_value = STREQ (local_dirname, *dirname) == 0;
462fb2
+	    }
462fb2
+      else
462fb2
+	      free (new_dirname);
462fb2
+	  dispose_words (wl);
462fb2
+	}
462fb2
+      else
462fb2
+	free (new_dirname);
462fb2
+    }	
462fb2
+
462fb2
+  return (return_value);
462fb2
+}
462fb2
+
462fb2
 /* Handle symbolic link references and other directory name
462fb2
    expansions while hacking completion.  This should return 1 if it modifies
462fb2
    the DIRNAME argument, 0 otherwise.  It should make sure not to modify