logoalt Hacker News

wpmyesterday at 6:39 PM2 repliesview on HN

If you're writing a zsh script and not worried about portability, you can also use the prompt expansion colors with "print".

    print_color () {
      print -P "%F{$1}$2%f"
    }
And then to use it

   print_color "green" "This is printed in green"

Replies

godelskiyesterday at 7:57 PM

Here's something also useful that's portable

  declare GRN='\e[1;32m'
  declare RED='\e[1;31m'
  declare YLW='\e[1;33m'
  declare CYN='\e[1;36m'

  write_log() {
      echo -e "[$( date +'%c' )] : ${1}\e[0m" | tee -a ${logfile}
  }

  write_log "${RED}I'm an ERROR"
leephillipsyesterday at 6:59 PM

Nice. I put this in my .zshrc.