Bash Color script output (cross-platform) color-output.sh

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

In the opening section of a bash script, it's possible to define some variables that function as helpers to color or otherwise format the terminal output during the run of the script.

Different platforms use different character sequences to express color. However, there's a utility called tput which works on all *nix systems and returns platform-specific terminal coloring strings via a consistent cross-platform API.

For example, to store the character sequence which turns the terminal text red or green:

red=$(tput setaf 1)
green=$(tput setaf 2)

Or, to store the character sequence which resets the text to default appearance:

reset=$(tput sgr0)

Then, if the BASH script needed to show different colored outputs, this can be achieved with:

echo "${green}Success!${reset}"
echo "${red}Failure.${reset}"


Got any Bash Question?