Ubuntu

bash-completion: readline expand-tilde not acknowledged

Reported by Gene Cumm on 2010-08-22
This bug report is a duplicate of:  Bug #324505: please do not expand leading tildes. Edit Remove
30
This bug affects 6 people
Affects Status Importance Assigned to Milestone
bash-completion (Ubuntu)
Undecided
Unassigned

Bug Description

Binary package hint: bash-completion

_expand() in /etc/bash_completion, a part of bash-completion, does not acknowledge the readline variable expand-tilde as of version 1.1-3ubuntu2.

Typing 'ls ~/<tab>' currently results in the command being expanded to 'ls /home/user/', even with readline's expand-tilde set to off.

Current _expand():
_expand()
{
    # FIXME: Why was this here?
    #[ "$cur" != "${cur%\\}" ] && cur="$cur\\"

    # Expand ~username type directory specifications. We want to expand
    # ~foo/... to /home/foo/... to avoid problems when $cur starting with
    # a tilde is fed to commands and ending up quoted instead of expanded.

    if [[ "$cur" == \~*/* ]]; then
        eval cur=$cur
    elif [[ "$cur" == \~* ]]; then
        cur=${cur#\~}
        COMPREPLY=( $( compgen -P '~' -u "$cur" ) )
        [ ${#COMPREPLY[@]} -eq 1 ] && eval COMPREPLY[0]=${COMPREPLY[0]}
        return ${#COMPREPLY[@]}
    fi
}

The problem lies in the area "eval cur=$cur" as it should only do this expansion if readline's expand-tilde is on.

The following fixes it:
_expand()
{
    # FIXME: Why was this here?
    #[ "$cur" != "${cur%\\}" ] && cur="$cur\\"

    # Expand ~username type directory specifications. We want to expand
    # ~foo/... to /home/foo/... to avoid problems when $cur starting with
    # a tilde is fed to commands and ending up quoted instead of expanded.

    if [[ "$cur" == \~*/* ]]; then
        if [ $(bind -v|grep 'set expand-tilde on'> /dev/null) ]; then
            eval cur=$cur
        else
            return 0;
        fi
    elif [[ "$cur" == \~* ]]; then
        cur=${cur#\~}
        COMPREPLY=( $( compgen -P '~' -u "$cur" ) )
        [ ${#COMPREPLY[@]} -eq 1 ] && eval COMPREPLY[0]=${COMPREPLY[0]}
        return ${#COMPREPLY[@]}
    fi
}

This function does perform the intended fix however there may be a more effective and efficient manner of resolving this. This still needs to be verified and tested further (for example, in the context of a script versus the command line).

If Ubuntu chooses to set the system-wide default of readline's expand-tilde to on, that would be acceptable so long as a user may then reconfigure it for a given system or user.

Gene Cumm (gene-cumm) on 2010-08-22
description: updated
Victor Zamanian (victorz) wrote :

I was trying to set my tilde-expansion to OFF all of last night and was up to 4 AM trying to figure out why my inputrc-file wasn't recognizing 'set expand-tilde Off'. GRRR, stupid bash_completion doing whatever it wants to without checking user settings first.

Victor Zamanian (victorz) wrote :

The change in the description does not perform the fix for me. However, the following change does. This is interrupting the __expand_tilde_by_ref() function instead of the _expand() function, depending on the expand-tilde option.

__expand_tilde_by_ref() {
+ if [ -z "$(bind -v | grep 'set expand-tilde on')" ]; then
+ return 0;
+ fi
    . . .
}

The logic is somewhat modified, but I tried this logic in the _expand function as well, and it didn't seem to fix it.

Victor Zamanian (victorz) wrote :

I see now that there is already a function defined in /etc/bash_completion specifically for this purpose, so now we get:

__expand_tilde_by_ref() {
    if ! _rl_enabled expand-tilde; then
        return 0
    fi
    . . .
}

Works like a charm.

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers