在 bash 脚本中提取 git 提交消息时如何保留行尾?

How to preserve line endings when extracting git commit messages in a bash script?

我正在使用 git svn 访问 SVN 服务器。 我正在尝试编写一个脚本,将所有 SVN 标签转换并复制到 git 标签中。

我发现了这个: http://gitready.com/advanced/2009/02/16/convert-git-svn-tag-branches-to-real-tags.html

还有这个:

要创建:

git for-each-ref refs/remotes/origin/tags | cut -d / -f 5- |
while read ref
do
    msg=`git log --format=%B -n 1 origin/tags/"$ref"`
    echo $msg
done

git log 命令在 mingw bash(来自 git 安装)中运行良好。 但是当从同一个 mingw bash 启动脚本时,行尾被删除。 我尝试添加 \r:

msg=`echo $msg | sed 's/\n/\r\n/'`

但是也没用...

有人可以帮我吗?谢谢!

至少尝试使用双引号:

echo "${msg}"

(参见“how do I preserve newlines in a quoted string in bash?”)

这将避免 echo 解释 $msg 内容的输出(执行 )。