4 ms·
Don't output ANSI colour codes directly - your output could redirect to a file, or perhaps the user simply prefers no colour. Use tput instead, and add a little
by haileys 2y ago
Don't output ANSI colour codes directly - your output could redirect to a file, or perhaps the user simply prefers no colour. Use tput instead, and add a little snippet like this to the top of your script:
command -v tput &>/dev/null && [ -t 1 ] && [ -z "${NO_COLOR:-}" ] || tput() { true; }
This checks that the tput command exists (using the bash 'command' builtin rather than which(1) - surprisingly, which can't always be relied upon to be installed even on modern GNU/Linux systems), that stdout is a tty, and that the NO_COLOR env var is not set. If any of these conditions are false, a no-op tput function is defined.
This little snippet of setup lets you sprinkle tput invocations through your script knowing that it's going to do the right thing in any situation.
- deleted 2y ago[deleted]
- ChoHag 2y ago[dead]
- deleted 2y ago[deleted]
- lilyball 2y agoIf you use tput a lot it's also worth caching the output, because invoking it for every single color change and reset can really add up. If you know you're going to use a bunch of colors up front you can just stuff them into vars RED=$(tput setaf 1) GREEN=$(tput setaf 2) RESET=$(tput sgr0)
- hinkley 2y agoThere should just be a command for this. Like echo with a color flag that does something if you’re in a tty.
- f1shy 2y agoWhy? Benefit? 10% of people hace problemas with colors. Depending on the terminal/background, you will produce bad/invisible output. Any good typesetting book will tell you not to use colors.
- irundebian 2y agoI have problems with non-colored output because it makes it harder to distinguish important from less important stuff.
- hinkley 2y agoWhy are you arguing against the entire thread as a reply to my comment?
- deleted 2y ago[deleted]
- CGamesPlay 2y agoBut since there isn’t, even if you make one, people won’t want to rely on it as a dependency.
- Aeolun 2y agoCan add it to bash?
- CGamesPlay 2y agoIt’s a bit more complicated. POSIX shell != bash, for example the default shell (/bin/sh) on macOS is now zsh, on Ubuntu it’s dash. Bash may still be installed, but may be years older than 2024. At a certain point, you’re better off embracing a dependency that just does everything better, like Python or Oil shell, for example.
- jph 2y agoYes and even better for faster speed and greater shell compatibility for basic colors, you can use this POSIX code: if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then COLOR_RESET='[0m' COLOR_RED='[31m' COLOR_GREEN='[32m' COLOR_BLUE='[34m' else COLOR_RESET='' COLOR_RED='' COLOR_GREEN='' COLOR_BLUE='' fi For more about this see Unix Shell Script Tactics: https://github.com/SixArm/unix-shell-script-tactics/tree/main/doc/use-color-with-terminal-escape-codes-and-no-color https://github.com/SixArm/unix-shell-script-tactics/tree/mai... Be aware there's an escape character at the start of each of color string, which is the POSIX equivalent of $'\e'; Hacker News seems to cut out that escape character.