当我打开终端时出现以下错误:
bash: /usr/share/bash-completion/bash_completion: line 171: syntax error near unexpected token `}'
bash: /usr/share/bash-completion/bash_completion: line 171: `}'
这是由于该bash-completion
文件的这个区域造成的:
_upvar()
{
if unset -v "$1"; then # Unset & validate varname
if (( $# == 2 )); then
eval $1=\"\$2\" # Return single value
else
eval $1=\(\"\${@:2}\"\) # Return array
fi
fi
}
为什么我会在那部分收到错误?
答案1
要查看您的别名是否与 bash 内置命令或系统命令冲突,请使用此脚本:
#!/bin/bash -i
# NAME: alias-check
# PATH: /mnt/e/bin
# DESC: Verify alias doesn't conflict with bash built-in
# DATE: June 17, 2018.
# Get aliases
alias > /tmp/alias.lst
sed -i 's/=.*//; s/alias //g' /tmp/alias.lst
#cat /tmp/alias.lst
while read -r line
do
command type -a ${line}
done< /tmp/alias.lst
测试不好的别名
编辑~/.bashrc
并插入这些“坏”别名:
alias fi=find
alias test='ls test'
现在运行脚本alias-check
:
bash: /home/rick/.bashrc: line 171: syntax error: unexpected end of file
alert is aliased to `notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e 's/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//')"'
cdh is aliased to `cd /home/rick'
cdo is aliased to `cd /mnt/old'
cdt is aliased to `cd /usr/share/grub/themes/Tuxkiller2'
egrep is aliased to `egrep --color=auto'
egrep is /bin/egrep
fgrep is aliased to `fgrep --color=auto'
fgrep is /bin/fgrep
fi is aliased to `find'
fi is a shell keyword
grep is aliased to `grep --color=auto'
grep is /bin/grep
l is aliased to `ls -CF'
la is aliased to `ls -A'
ll is aliased to `ls -alF'
ls is aliased to `ls --color=auto'
ls is /bin/ls
lsdrv is aliased to `lsblk -o NAME,FSTYPE,LABEL,MOUNTPOINT,SIZE,MODEL'
redalert is aliased to `notify-send --urgency=critical -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e 's/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//')"'
sudo is aliased to `sudo '
sudo is /usr/bin/sudo
test is aliased to `ls test'
test is a shell builtin
test is /usr/bin/test
zenity is aliased to `zenity 2>/dev/null'
zenity is /mnt/e/usr/local/bin/zenity
zenity is /usr/local/bin/zenity
zenity is /usr/bin/zenity
请注意以下几行:
- fi 是shell 关键字
- 测试是一个shell 内置命令
在您的别名中查找这些类型的错误。
测试后,不要忘记删除别名fi
并再次test
保存~/.bashrc
。
您不能通过在命令行上简单地发出别名来运行测试,因为脚本的第一行#!/bin/bash -i
使用加载了新的 shell ~/.bashrc
。
答案2
我认为这可能是同一个问题这篇 U&L 帖子:您有 的别名fi
。大多数情况下,该脚本都很好,但是为 shell 关键字设置别名可能会导致意外问题:
$ alias fi=find
$ . /usr/share/bash-completion/bash_completion
bash: /usr/share/bash-completion/bash_completion: line 171: syntax error near unexpected token `}'
bash: /usr/share/bash-completion/bash_completion: line 171: `}'
尽管该函数位于第 170 行左右,但它是该文件中第一次使用 的地方fi
。根据您放置此别名定义的确切位置以及 bash 确切获取 bash 完成脚本的位置,这可能是设置该别名后第一次fi
使用。