color-output.sh

suggest change

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}"

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents