Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I have a similar section in my .bashrc via a somewhat hackish loop (or two):

  ESC=$'\x1b'

  # Terminal color escapes
  colornames=(BLACK RED GREEN YELLOW BLUE MAGENTA CYAN)
  for i in ${!colornames[@]}; do
  	eval "${colornames[$i]}=\"${ESC}[3${i}m\""
  	eval "BG${colornames[$i]}=\"${ESC}[4${i}m\""
  done

  # There are holes in this sequence, so it doesn't loop nicely
  BOLD="${ESC}[1m"
  DARK="${ESC}[2m"
  UNDERLINE="${ESC}[4m"
  BLINK="${ESC}[5m"
  INVERSE="${ESC}[7m"
  RST="${ESC}[m"

  styles=(BOLD DARK UNDERLINE BLINK INVERSE RST)

  # and the PS1-safe versions of the above
  for i in ${colornames[@]} ${colornames[@]/#/BG} ${styles[@]}; do
  	eval "PS$i=\"\\[\$$i\\]\""
  done
Having color escapes in shell variables comes in handy from time to time (like faking 'colordiff' with sed).


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: