git - 通过 shell 变量将配置选项传递给 git clone

标签 git bash shell environment-variables

我正在尝试通过 bash 上的环境变量将动态创建的配置选项传递给 git clone

直接传递它们可以,但通过环境变量不起作用:

$ git clone -c 'url.foo.insteadof=bar' git://git.cweiske.de/psist.git
... all fine

$ export PARAMS="-c 'url.foo.insteadof=bar'"; git clone $PARAMS git://git.cweiske.de/psist.git
error: invalid key: 'url.foo.insteadof

我该怎么做才能让 git 识别这些选项?

最佳答案

这是因为在第一个示例中 quotes语法:

$ (set -o xtrace; git clone -c 'url.foo.insteadof=bar' git://invalid) 2>&1 | grep 'git clone'
+ git clone -c url.foo.insteadof=bar git://invalid

而在第二个中,它们是字面意思:

$ (set -o xtrace; export PARAMS="-c 'url.foo.insteadof=bar'" && git clone $PARAMS git://invalid) 2>&1 | grep 'git clone'
+ git clone -c ''\''url.foo.insteadof=bar'\''' git://invalid

您可以use arrays to reliably pass arguments using variables :

$ (set -o xtrace; export PARAMS=('-c' 'url.foo.insteadof=bar') && git clone "${PARAMS[@]}" git://invalid) 2>&1 | grep 'git clone'
+ git clone -c url.foo.insteadof=bar git://invalid

关于git - 通过 shell 变量将配置选项传递给 git clone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31471237/

相关文章:

c++ - qt5.4在linux for windows上交叉编译

git - 您如何构建 Git 存储库工作流程?

linux - Loop on Unix,几何图形的基本计算器

linux - sed:未终止的地址正则表达式

bash - bash 中的字符串比较。 [[ : not found

linux - 在 Linux 中对两个文件进行排序并找到每个文件唯一的行

git - 禁止git中的外部文件

bash - 使用 bash 命令(带管道)的输出作为另一个命令的参数

python - 我怎样才能知道 os.popen 是否在同一个 shell 中运行 linux 命令?

Git 从另一个远程分支更新我的远程分支