Indenting multi-line output in a shell script

前端 未结 3 807
陌清茗
陌清茗 2021-01-30 19:28

I\'m trying to change the message of the day (MOTD) on my Ubuntu Amazon EC2 box so that it will display the git status of one of my directories when I SSH in.

The output

相关标签:
3条回答
  • 2021-01-30 20:06

    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
    
    0 讨论(0)
  • 2021-01-30 20:09

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

    git status | sed 's/^/  /'
    
    0 讨论(0)
  • 2021-01-30 20:13

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

    indent() { sed 's/^/  /'; }
    
    git status | indent
    other_command | indent
    
    0 讨论(0)
提交回复
热议问题