首页 > 解决方案 > 通过 xargs util 将配置参数传递给 git 命令

问题描述

我正在尝试构建一个xargs用于传递配置参数的命令:user.nameuser.emailto git commit

要构建的命令xargs

git -c user.name=abc -c user.email=abc@mail.com commit

我尝试过的:

echo "-c user.name=abc -c user.email=abc@mail.com" | xargs -I % git % commit

但是,git返回这个:

未知选项:-c user.name=abc -c user.email=abc@mail.com

即使xargs冗长,该命令也可以正常工作。

echo "-c user.name=abc -c user.email=abc@mail.com" | xargs -tI % git % commit

这将打印要执行的命令,该命令git -c user.name=abc -c user.email=abc@mail.com commit在复制粘贴到终端时起作用。

请注意,配置参数由空格分隔。

通过传递配置参数我正在尝试做什么的一些上下文

标签: gitxargs

解决方案


根据评论,xargs正在用单个参数替换%单个参数-c user.name=abc -c user.email=abc@mail.com;结果命令

git -c user.name=abc -c user.email=abc@mail.com commit

有两个参数,第一个是-c user.name=abc -c user.email=abc@mail.com,显然是一个无效的选项。

我能想到的最便携的解决方法是让 shell 重新解释该行:

echo "-c user.name=abc -c user.email=abc@mail.com" | xargs -I % bash -c "git % commit"

这样,xargsbash使用两个参数执行:-cgit -c user.name=abc -c user.email=abc@mail.com commitbash -c command执行该命令,其中包括bash通常执行的完整命令行解析。这将导致使用五个参数bash执行: 、、和。git-cuser.name=abc-cuser.email=abc@mail.comcommit


推荐阅读