_expand 有什么作用?

scr*_*lli 12 command-line bash functions auto-completion

我正在使用外壳程序,并且错误地tab在编写后自动完成了_e,这导致_expand.

这个命令有什么作用?我在网上找不到解释,我在 Ask Ubuntu 上能找到的唯一参考是:

但他们没有回答我的问题。相反,他们开辟的同类更有人对类似这样的命令_complete_complete_as_root等等。

cha*_*aos 14

你可以_expand在打字时找出什么

$ 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
}
Run Code Online (Sandbox Code Playgroud)

这是 bash 完成机制中的一个函数。它~在路径名中扩展波浪号 ( )。In/etc/bash_completion是关于函数的注释:

# 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.
Run Code Online (Sandbox Code Playgroud)

在终端中尝试,输入:

~<tab><tab>
Run Code Online (Sandbox Code Playgroud)

它将扩展到用户名,例如

~usera     ~userb     ~userc
Run Code Online (Sandbox Code Playgroud)

  • 正是 `_expand` 函数,就像所有其他完整的函数一样,只是根据包含完成模式的 `$cur` 的值来操作 `COMPREPLY` 数组。 (3认同)