我正在尝试调试位于/usr/share/bash-completion/bash_completion
(每个发行版上的 bash 完成包)中的自动完成脚本
export PS4='+'$'\t''$LINENO'$'\t # \t for proper indentation
set -x
在里面当前外壳,然后键入命令(后跟空格),然后按 Tab 调用自动完成。
这是结果的一部分:
+ 2205 for dir in "${dirs[@]}"
+ 24 [[ -d /usr/share/bash-completion/completions ]]
+ 2207 for compfile in "$cmd" "$cmd.bash" "_$cmd"
+ 26 compfile=/usr/share/bash-completion/completions/bittch
+ 28 [[ -f /usr/share/bash-completion/completions/bittch ]]
+ 2207 for compfile in "$cmd" "$cmd.bash" "_$cmd"
正如您所看到的,第一行号是2205
脚本内的绝对行号。令人惊讶的是,下一行(这是一个嵌套块)的编号是24
,即相对于函数本身的行号(见下文)
__load_completion()
{
local -a dirs=(${BASH_COMPLETION_USER_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion}/completions)
local ifs=$IFS IFS=: dir cmd="${1##*/}" compfile
[[ -n $cmd ]] || return 1
for dir in ${XDG_DATA_DIRS:-/usr/local/share:/usr/share}; do
dirs+=($dir/bash-completion/completions)
done
IFS=$ifs
if [[ $BASH_SOURCE == */* ]]; then
dirs+=("${BASH_SOURCE%/*}/completions")
else
dirs+=(./completions)
fi
local backslash=
if [[ $cmd == \\* ]]; then
cmd="${cmd:1}"
# If we already have a completion for the "real" command, use it
$(complete -p "$cmd" 2>/dev/null || echo false) "\\$cmd" && return 0
backslash=\\
fi
for dir in "${dirs[@]}"; do
[[ -d $dir ]] || continue
for compfile in "$cmd" "$cmd.bash" "_$cmd"; do
compfile="$dir/$compfile"
# Avoid trying to source dirs; https://bugzilla.redhat.com/903540
if [[ -f $compfile ]] && . "$compfile" &>/dev/null; then
[[ $backslash ]] && $(complete -p "$cmd") "\\$cmd"
return 0
fi
done
done
# Look up simple "xspec" completions
[[ -v _xspecs[$cmd] ]] &&
complete -F _filedir_xspec "$cmd" "$backslash$cmd" && return 0
return 1
}
我知道这LINENO
代表当前执行的脚本或 shell 函数中的行号。但它们位于同一函数中,因此两者都应该是相对的(相对于 的开头__load_completion()
)或绝对的。
为什么是这样?