Colors for POSIX compliant shell scripts without any dependencies or fuss.
Because it is POSIX compliant, all the shells are supported. E.g. Bash, Zsh, SH, Ksh and Dash.
Basic formatting such as bold and underline is also supported!
The script automatically disables colors when output is NOT a terminal.
E.g. when outputting to a file no weird escape sequences (\033[3m
) is included.
Conversely when outputting to the terminal, all those beautiful colors shine through!
However this behaviour can be customised. See below
Tip
See example.sh for how to use.
You can either simply copy the contents of posix-colors into your script. Or you can download this repo and source the file like so:
. /path/to/posix-colors
Or for bash and zsh you alternatively use:
source /path/to/posix-colors
As mentioned above this script supports automatic coloring depending on whether or not output is a tty.
This means when viewing output from the script directly, colors will be displayed.
But when redirecting to a file or piping to other programs the color escape codes are automatically disabled.
So you won't get those weird escape sequences.
It follows the standard proposed here: clicolors
That means if CLICOLOR_FORCE
is set in the environment before the script is run or sourced,
then the color escape sequences are always used.
For instance seeing color with less:
CLICOLOR_FORCE=true ./example.sh | less -R
Conversely can color output be completely disabled regardless of tty by setting NO_COLOR
env variable:
NO_COLOR=true ./example.sh