在〜/ .bashrc文件中创建“本地”别名后,自动填充失败

时间:2019-07-23 16:06:10

标签: bash ubuntu alias autofill

我在.bashrc文件中添加了别名。我这样做并运行源〜/ .bashrc之后,自动填充开始在Ubuntu命令提示符下引发错误。该错误将我定向到bash_completion文件的第295行,以查找错误的语法。

当我打开此文件并转到第295行时,没有看到不正确的语法。这是我要添加的代码行以及我收到的错误。

我最初添加到我的.bashrc文件中的别名:

alias local='cd /mnt/c/Users/NYCZE/OneDrive/'

运行source ~/.bashrc来实现此别名时引发错误:

bash: /usr/share/bash-completion/bash_completion: line 295: syntax error near unexpected token `('
bash: /usr/share/bash-completion/bash_completion: line 295: `('

在我的bash_completion文件中包含第295行的函数:

__reassemble_comp_words_by_ref()
{
    local exclude i j line ref  # This line right here is the problem one
    # Exclude word separator characters?
    if [[ $1 ]]; then
        # Yes, exclude word separator characters;
        # Exclude only those characters, which were really included
        exclude="${1//[^$COMP_WORDBREAKS]}"
    fi

    # Default to cword unchanged
    printf -v "$3" %s "$COMP_CWORD"
    # Are characters excluded which were former included?
    if [[ $exclude ]]; then
        # Yes, list of word completion separators has shrunk;
        line=$COMP_LINE
        # Re-assemble words to complete
        for (( i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
            # Is current word not word 0 (the command itself) and is word not
            # empty and is word made up of just word separator characters to
            # be excluded and is current word not preceded by whitespace in
            # original line?
            while [[ $i -gt 0 && ${COMP_WORDS[$i]} == +([$exclude]) ]]; do
                # Is word separator not preceded by whitespace in original line
                # and are we not going to append to word 0 (the command
                # itself), then append to current word.
                [[ $line != [[:blank:]]* ]] && (( j >= 2 )) && ((j--))
# Append word separator to current or new word
                ref="$2[$j]"
                printf -v "$ref" %s "${!ref}${COMP_WORDS[i]}"
                # Indicate new cword
                [[ $i == $COMP_CWORD ]] && printf -v "$3" %s "$j"
                # Remove optional whitespace + word separator from line copy
                line=${line#*"${COMP_WORDS[$i]}"}
                # Start new word if word separator in original line is
                # followed by whitespace.
                [[ $line == [[:blank:]]* ]] && ((j++))
                # Indicate next word if available, else end *both* while and
                # for loop
                (( $i < ${#COMP_WORDS[@]} - 1)) && ((i++)) || break 2
            done
            # Append word to current word
            ref="$2[$j]"
            printf -v "$ref" %s "${!ref}${COMP_WORDS[i]}"
                # Indicate new cword
                [[ $i == $COMP_CWORD ]] && printf -v "$3" %s "$j"
                # Remove optional whitespace + word separator from line copy
                line=${line#*"${COMP_WORDS[$i]}"}
                # Start new word if word separator in original line is
                # followed by whitespace.
                [[ $line == [[:blank:]]* ]] && ((j++))
                # Indicate next word if available, else end *both* while and
                # for loop
                (( $i < ${#COMP_WORDS[@]} - 1)) && ((i++)) || break 2
            done
            # Append word to current word
            ref="$2[$j]"
            printf -v "$ref" %s "${!ref}${COMP_WORDS[i]}"
            # Remove optional whitespace + word from line copy
            line=${line#*"${COMP_WORDS[i]}"}
            # Indicate new cword
            [[ $i == $COMP_CWORD ]] && printf -v "$3" %s "$j"
        done
        [[ $i == $COMP_CWORD ]] && printf -v "$3" %s "$j"
    else
        # No, list of word completions separators hasn't changed;
        for i in ${!COMP_WORDS[@]}; do
            printf -v "$2[i]" %s "${COMP_WORDS[i]}"
        done
    fi
}

2 个答案:

答案 0 :(得分:1)

local是一个保留关键字,在许多Bash完成包中广泛使用,并且通常在具有功能的Bash代码中使用。您真的不想覆盖它,因为那样会破坏那些程序包。别称您的别名。

(也许也根本不使用别名-Shell函数的用途更加广泛-但这不会改变问题的实质。)

答案 1 :(得分:0)

i是内置的shell。您可以从

看到
local

在您的别名之后,它已变成type local 。现在,相同的cd命令将给出

type

现在,我的/ usr / share / bash-completion / bash_completion文件中的第295行是

local is aliased to `cd /mnt/c/Users/NYCZE/OneDrive/'

因此,local cword words=() 的行为不再符合该文件的预期。文件中的行类似地使用local