You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

重载Bash命令:向原二进制程序传递参数失败问题排查

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
    # 用双引号包裹$@,保留原始参数结构
    /usr/bin/git "$@"
fi

$@带引号和不带引号的差异

  • 不带引号的$@:Bash会将所有参数按空白字符(空格、制表符、换行)拆分,不管原参数是否被引号包裹。比如你输入git commit -m "Init HW4 Rust project",脚本会把参数拆成commit、-m、Init、HW4、Rust、project,这会让Git误将Init及后续内容当作文件路径,从而报错路径不匹配。
  • 带双引号的"$@":Bash会严格保留每个原始参数的边界,带空格的参数会作为一个整体传递,效果和你直接在命令行输入原生Git命令完全一致。上面的例子中,传给Git的参数会是commit、-m、Init HW4 Rust project,和直接调用/usr/bin/git commit -m "Init HW4 Rust project"的参数结构完全相同,就能正常执行。

内容的提问来源于stack exchange,提问作者Robbie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 04:53:14