我希望以与在终端上运行命令时输出的格式相同的格式回显命令的输出,但出于某种原因,使用echo似乎消除了换行符。
示例:
$ OUTPUT=$(git status)
$ echo $OUTPUT
# On branch feature_install # Untracked files: # (use "git add <file>..." to include in what will be committed) # # install/ nothing added to commit but untracked files present (use "git add" to track)
但这应该打印出来:
$ git status
# On branch feature_install
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# install/
nothing added to commit but untracked files present (use "git add" to track)
此外,可以在解析的输出中保持颜色吗? (使用回声,不保持颜色)
答案 0 :(得分:10)
如果使用双引号,则会保留换行符:
echo "$OUTPUT"
关于颜色:如果输出不是tty,git不输出颜色代码。要强制使用颜色代码,您可以执行以下操作:
OUTPUT=$( GIT_PAGER_IN_USE=true git status )