12

I was working with the shell, and by mistake I autocompleted with tab after writing _e, which resulted in _expand.

What does this command do? I couldn't find an explanation online, the only references I could find here on Ask Ubuntu were:

But they don't answer my question. Instead, they open up more questions of the same kind about commands like _complete, _complete_as_root, etc.

1 Answer 1

14

You can find out what _expand does, when typing

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

This is a function in the bash completion mechanism. It expands tildes (~) in pathnames. In /etc/bash_completion is a comment about the function:

# 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.

Try it in a terminal, type:

~<tab><tab>

It will expand to the usernames, for example

~usera     ~userb     ~userc
2
  • 1
    Thanks, I didn't know the command type. It wasn't clear to me why I could not call these functions as _function_name [argument], but now I understand that they serve their purpose as autocomplete extensions, and the fact that they appear in my autocomplete is simply because they are declared (but they are not meant to be called directly).
    – 0x5C91
    Sep 29, 2015 at 8:50
  • 3
    Exactly the _expand function, as all other complete functions, just manipulates the COMPREPLY array, based on values of $cur which contains the completion pattern.
    – chaos
    Sep 29, 2015 at 8:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .