最近git branch <tab>
开始向我显示以下错误:
$ git branch bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `/usr/bin/git --git-dir=.git for-each-ref --format=%(refname:short) refs/tags refs/heads refs/remotes'
HEAD bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `/usr/bin/git --git-dir=.git for-each-ref --format=%(refname:short) refs/tags refs/heads refs/remotes'
HEAD ^C
我该如何修复它?
我的 中有以下几行~/.bashrc
:
git() {
cmd=$1
shift
extra=""
quoted_args=""
whitespace="[[:space:]]"
for i in "$@"
do
if [[ $i =~ $whitespace ]]
then
i=\"$i\"
fi
quoted_args="$quoted_args $i"
done
cmdToRun="`which git` "$cmd" $quoted_args"
cmdToRun=`echo $cmdToRun | sed -e 's/^ *//' -e 's/ *$//'`
bash -c "$cmdToRun"
# Some mad science here
}
答案1
您的脚本不保留引号。完成后执行的原始行是:
git --git-dir=.git for-each-ref '--format=%(refname:short)' refs/tags refs/heads refs/remotes
通过你的脚本你得到:
bash -c '/usr/bin/git --git-dir=.git for-each-ref --format=%(refname:short) refs/tags refs/heads refs/remotes'
请注意周围缺少的引号:
--format=%(refname:short)
没有看过你实际做了什么,但是这个:
quoted_args="$quoted_args \"$i\""
# | |
# +--+------- Extra quotes.
应该会产生如下结果:
bash -c '/usr/bin/git --git-dir=.git "for-each-ref" "--format=%(refname:short)" "refs/tags" "refs/heads" "refs/remotes"'
或者:
quoted_args="$quoted_args '$i'"
# | |
# +--+------- Extra quotes.
bash -c '/usr/bin/git --git-dir=.git '\''for-each-ref'\'' '\''--format=%(refname:short)'\'' '\''refs/tags'\'' '\''refs/heads'\'' '\''refs/remotes'\'''
您可能想查看%q
的格式printf
。