21

I am attempting to write a bash script and I am having difficulty making the output look neat and organized. I could fall back on just using newlines, but I would much rather have output that was easy to read. For instance, when I run git clone ..., I want to first echo "Cloning repository" and then have the output of git indented. Example output:

Cloning repository...
    Initialized empty Git repository in /root/client_scripts/jojo/.git/
    remote: Counting objects: 130, done.
    remote: Compressing objects: 100% (121/121), done.
    remote: Total 130 (delta 13), reused 113 (delta 6)
    Receiving objects: 100% (130/130), 176.07 KiB, done.
    Resolving deltas: 100% (13/13), done.

Currently, it's all compressed with no indentation. Does anyone know how to do this? I attempted with sed and awk but it didn't seem to show any more output than just Initialized empty Git repository in /root/client_scripts/jojo/.git/. I would greatly appreciate any comments.

5 Answers 5

48

Pipe through

sed "s/^/    /g"

This will replace the (zero-width) anchor for line start by four spaces, effectively adding four spaces at the start of the line.

(The g does this globally; without it, it will only do it once, which would do the first line.)

Sign up to request clarification or add additional context in comments.

9 Comments

You could put a literal tab character in there too, if that's what you want.
I already tried this, unfortunately, it outputs only the first line ("Initialized empty..."). My guess is that the rest of the information is output to STDERR but I can't seem to figure out how to redirect it.
Use I/O redirection to capture STDERR
@Topher: a quick way to determine what's going to stdout is command | less : less will only catch stdout, and you won't see anything that went to stderr. Well, it might show up until you press j/k/up/down to "refresh" the page.
For the original issue of indentation being applied only to the first line: You have to append a "g" at the end of the sed string: sed 's/^/ /g'
|
9

A different solution that doesn't require sed:

command | (while read; do echo "    $REPLY"; done)

2 Comments

You're missing the read variable; fix: `comand | while read REPLY; do echo " $REPLY"; done'
@Ryan Bright: No, read the Bash manual, the REPLY variable is implied. Or just type this in a shell and test it for yourself.
7

The problem with piping the output of git through any command is that git will detect that the output is not a terminal so it won't output messages which are progress messages because (typically) it is not useful to pipe a whole lot of terminal characters and progress updates to something that isn't a terminal.

To get the progress messages anyway you need to provide the --verbose option to git clone. The progress messages appear on stderr so you are likely to need a pipe something like 2>&1 | ....

Be aware the the progress messages won't appear line by line, but you'll get a lot of terminal escape codes which are designed to clear the same line. Trying to indent this output by piping through a line based tool like sed is likely to prove difficult, if not impossible. For a program that can handles input unbuffered, it should be fairly possible to look for a ^M in the output and add some spaces (or a tab) immediately aftwards, flushing as often as each batch of data is received.

2 Comments

Thanks for the information, however, neither my version of git (1.5.6.5) nor the latest have --version as an option to either git or git-clone. I guess I am simply out of luck if I want to do this. Thanks for the info on git detecting that it isn't on a terminal.
Err...I just reread my previous comment, I really did mean --verbose not --version.
6

You can filter the output from the command you want to indent through sed.

/tmp/test>cat script
#!/bin/sh

echo "Running ls -l"
ls -l 2>&1 | sed 's/^/\t/'

/tmp/test>sh script
Running ls -l
        total 4
        -rw-rw-r-- 1 hlovdal hlovdal 55 2009-11-03 23:36 script
/tmp/test>

The sed command will replace the beginning of the line (before the first character) with a tabulator, i.e. insert a tabulator at the very beginning of the line.

Updated to also indent stderr.

1 Comment

Unfortunately, this doesn't capture the necessary output from git. Still only outputs the first line...
5

Since the awk solution wasn't posted yet:

$ echo -en "hello\nworld\n"
hello
world
$ echo -en "hello\nworld\n" | awk '{print "    "$0}'
    hello
    world

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.