title: How to right align and left align text strings in Bash
body: <p>I'm creating a bash script and would like to display a message with a right aligned status (OK, Warning, Error, etc) on the same line.</p>
  
  <p>Without the colors, the alignment is perfect, but adding in the colors makes the right aligned column wrap to the next line, incorrectly.</p>
  
  <pre><code>#!/bin/bash
  
  log_msg() {
      RED=$(tput setaf 1)
      GREEN=$(tput setaf 2)
      NORMAL=$(tput sgr0)
      MSG="$1"
      let COL=$(tput cols)-${#MSG}
  
      echo -n $MSG
      printf "%${COL}s"  "$GREEN[OK]$NORMAL"
  }
  
  log_msg "Hello World"
  exit;
  </code></pre>