I have a Bash script that I run inside of iTerm 2.1.3 on a Mac. I like to use color codes to change the color of the script's output if there's an error:
#!/usr/bin/env bash
database=mydb
RED='\033[0;31m'
echo -e "[Dumping database '$database']"
pg_dump -Fc -v -f /tmp/${database}.bak ${database}
if [ "$?" -ne 0 ]; then
printf "\n${RED}Database dump failed${NC}\n"
exit 1
fi
The problem is that if there's an error when I run this script from the Unix command line, then my command line takes on the color of the error output (in this case red) and I have to close the iTerm window to revert back to my usual color. Is there a way to prevent this from occurring inside my script?
Thanks.
${NC}?NC=$'\e[0m'should do it. (add that right below theRED=...line)NC=$'\e[0m'... format quotes instead of single quotes.