2012-02-02 83 views
1

我想以与在终端上运行命令时输出的格式相同的格式来回显命令的输出,但由于某些原因,使用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) 

此外,可以颜色被保持在所解析的输出? (使用回声,彩色不保)

+1

颜色从未出现过。 git注意到输出不是tty,也没有打印颜色的转义码。 – 2012-02-02 16:29:01

+0

在你的OUTPUT-line之前试试'IFS ='\ n'',看看它是否有帮助。 – bos 2012-02-02 16:35:28

+0

参见http://unix.stackexchange.com/questions/17732/where-has-the-trailing-newline-char-gone-from-my-command-substitution – 2012-02-02 16:36:40

回答

10

如果使用双引号,新行会维持:

 
echo "$OUTPUT" 

至于颜色:Git不会输出颜色代码,如果输出是不是tty 。要强制颜色代码,您可以执行以下操作:

 
OUTPUT=$(GIT_PAGER_IN_USE=true git status)