On 4/19/11 2:06 AM, Peter Toft wrote: > On Mon, 18 Apr 2011 20:12:24 -0400, Chet Ramey wrote: >> On 4/14/11 6:19 PM, Peter Toft wrote: >> >>> I have an annoying bash-problem on Red Hat Linux 5.x. If I e.g. try to move >>> to a subdirectory of another directory (e.g. $HOME), where the tab-expand >>> works poorly; >>> >>> Assume $HOME=/home/pto >> >> You should see whether or not you have a completion already defined by >> running `complete -p cd'. It would also help to know the version of bash >> you're using. That will help establish a baseline. (And RHL 5.x? That's >> pretty old.) > > Right! bash --version gives -> 3.2.25(1)-release (x86_64-redhat-linux-gnu) > > RH5.6 is not cutting edge (but I need it for tools-reasons), but I also > dislike > the current working method on e.g. Ubuntu 10.10, so I really like to get > into the dirt on this one :)
OK, here's the bottom line. By default, with no completion specifications installed for a particular command, bash will recognize a leading `$' and attempt completion of shell variable names. Only the name is completed: bash does not expand the variable to check whether or not the value is a directory name and does not treat it as a filename. You can write a shell function and assign it as the completion spec for cd, maybe augmenting the one that is distributed as part of the bash_completion package I suspect you have installed on Ubuntu. That function already does a pretty good job of handling all the possible cases. That part of the function would look something like (totally untested and off the cuff): case "$cur" in \$*) dirs=( $(compgen -v ${cur:1}) ) # complete to all variable names for d in "${dirs[@]}" ; do if eval test -d \"\$${d}\"; then compopt +o filenames # suppress quoting COMPREPLY+=(\$"${d}"/) fi done ;; *) ;; # whatever else esac >>> "cd $HOME<TAB>" is expanded to "cd /home/pto " (without the quotes). >>> I get $HOME expanded - quite ok - but I get an annoying space efter the >>> path. I can't reproduce this on bash-3.2.39. I get the same sort of variable name completion unless I add the trailing slash manually. Chet -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, ITS, CWRU c...@case.edu http://cnswww.cns.cwru.edu/~chet/