#!/bin/sh
LOG='log --pretty=format:"%h - %an, %ar : %s"'
git $LOG
我希望这能以指定格式输出实际的 git 日志。然而,我得到的只是一个致命的参数错误。
我也尝试了以下选项,但它们也不起作用:
LOG="log --pretty=format:\"%h - %an, %ar : %s\""
LOG='log --pretty=format:\"%h - %an, %ar : %s\"'
然而,奇怪的是下面的脚本确实有效,我不明白为什么:
LOG='--pretty=format:"%h - %an, %ar : %s"'
git log "$LOG"
有人认为 shell 认为变量只是一个参数,但是下面的工作正常:
LOG1LINE='log --pretty=oneline'
git $LOG1LINE
最佳答案
这是克服这个问题的一种方法。使用 bash 数组:
#!/bin/bash
#It's important to use bash instead of sh for this to work
LOG=(log --pretty=format:"%h - %an, %ar : %s")
git "${LOG[@]}"
关于bash - Bash Shell 变量中的引号不起作用,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17814246/