2012-02-02 94 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)