Highway of Life
Highway of Life

Reputation: 24321

How to echo multi-line output from captured command?

I would like to echo the output from a command in the same format that it would output when I run a command on the terminal, but for some reason, using echo seems to eliminate newlines.

Example:

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

But this should have printed:

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

Additionally, can color be maintained in the parsed output? (Using echo, color was not maintained)

Upvotes: 1

Views: 1550

Answers (1)

William Pursell
William Pursell

Reputation: 212238

If you use double quotes, the newlines will be maintained:

echo "$OUTPUT"

As to the color: git does not output color codes if the output is not a tty. To force the color codes, you can do:

OUTPUT=$( GIT_PAGER_IN_USE=true git status )

Upvotes: 10

Related Questions