bash-completion: readline expand-tilde not acknowledged

Bug #622403 reported by Gene Cumm
This bug report is a duplicate of:  Bug #324505: please do not expand leading tildes. Edit Remove
32
This bug affects 7 people
Affects Status Importance Assigned to Milestone
bash-completion (Ubuntu)
Confirmed
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)
description: updated
Revision history for this message
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.

Revision history for this message
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.

Revision history for this message
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.

Revision history for this message
Launchpad Janitor (janitor) wrote :

Status changed to 'Confirmed' because the bug affects multiple users.

Changed in bash-completion (Ubuntu):
status: New → Confirmed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.