【问题标题】:How to echo multi-line output from captured command?如何从捕获的命令中回显多行输出?
【发布时间】:2012-02-02 16:26:28
【问题描述】:

我想以与我在终端上运行命令时输出的格式相同的格式回显命令的输出,但出于某种原因,使用回显似乎可以消除换行符。

例子:

$ 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)

另外,可以在解析的输出中保持颜色吗? (使用echo,颜色不保持)

【问题讨论】:

  • 颜色从来没有出现过。 git 注意到输出不是 tty 并且没有打印颜色的转义码。
  • 在您的 OUTPUT 行之前尝试 IFS='\n' 看看是否有帮助。

标签: git bash shell


【解决方案1】:

如果您使用双引号,则将保留换行符:

回声“$ 输出”

关于颜色:如果输出不是 tty,git 不会输出颜色代码。要强制使用颜色代码,您可以这样做:

OUTPUT=$(GIT_PAGER_IN_USE=true git 状态)

【讨论】:

    猜你喜欢
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 2018-01-28
    相关资源
    最近更新 更多