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)
If you use double quotes, the newlines will be maintained:
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: