重载 Bash 命令:将参数传递给原始二进制文件不起作用



我正在编写一个简单的bash脚本,为git status添加一些额外的功能。目前,我正在通过将以下脚本(名为git)添加到我的路径中来实现这一点:

#!/bin/bash
# Simple script to automatically configure user info between multiple repositories
#  by overloading the functionality of 'git status'
if [[ "$1" == "status" ]]; then
# do something extra, then run vanilla 'git status'
/usr/bin/git status
else
# behave normally
/usr/bin/git $@
fi

当我输入git <any args>时,Bash首先找到了名为git的脚本,因此它运行该脚本。我遇到的问题是其他条款。显然,在这样的脚本中运行/usr/bin/git $@与在命令行中运行它不同。例如,当我运行git-commit:时

$ git commit -m "Init HW4 Rust project" # runs the else branch of my script
error: pathspec 'HW4' did not match any file(s) known to git
error: pathspec 'Rust' did not match any file(s) known to git
error: pathspec 'project' did not match any file(s) known to git
$ /usr/bin/git commit -m "Init HW4 Rust project" # runs my standard git installation
On branch main
Your branch is ahead of 'origin/main' by 2 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean

我怀疑这与$@变量有关。据我所知,$@表示Bash脚本接收到的所有命令行参数,但第0个参数(命令)除外。这与显式键入参数有什么不同?

因为没有引用$@,所以得到的参数比预期的要多:

$ set -- commit -m "Init HW4 Rust project"
$ printf '%qn' $@
commit
-m
Init
HW4
Rust
project

将其与正确引用的"$@":进行比较

$ printf '%qn' "$@"
commit
-m
Init HW4 Rust project