Indenting multi-line output in a shell script

Pipe it to sed to insert 2 spaces at the beginning of each line.

git status | sed 's/^/  /'

Building on @Barmar's answer, this is a tidier way to do it:

indent() { sed 's/^/  /'; }

git status | indent
other_command | indent

Thanks to @Barmar and @Marplesoft for some nice simple solutions - here is another variation that others might like - a function you can tell how many indent levels using pr:

indent() {
  local indentSize=2
  local indent=1
  if [ -n "$1" ]; then indent=$1; fi
  pr -to $(($indent * $indentSize))
}

# Example usage
ls -al | indent
git status | indent 2

Tags:

Linux

Shell

Bash