Take this command for example:
composer show drupal/core --latest | grep 'latest'
How could I make its output with a yellow background color?
Take this command for example:
composer show drupal/core --latest | grep 'latest'
How could I make its output with a yellow background color?
I don't know about yellow background, but the default is usually red text, with e.g. grep --color=auto 'latest'
or grep --color=always 'latest'
(which highlights only matched pattern)
try the following
# Set background color to yellow
tput setab 3 # background for everything that follows will be yellow
man tput | head -20
tput(1) tput(1)
NAME
tput, reset - initialize a terminal or query terminfo database
SYNOPSIS
tput [-Ttype] capname [parms ... ]
tput [-Ttype] init
tput [-Ttype] reset
tput [-Ttype] longname
tput -S <<
tput -V
DESCRIPTION
The tput utility uses the terminfo database to make the values of ter-
minal-dependent capabilities and information available to the shell
(see sh(1)), to initialize or reset the terminal, or return the long
name of the requested terminal type. The result depends upon the capa-
tput sgr0 #reset to 'normal'
@droplet_lover are you wanting to change the background color for just the given command(s) or the entire screen?
I use some terminal control sequences Operating System Command #11 control sequence ${OSC}11;#RRGGBB${ST}
frequently. But they change the entire screen, not just the specific command.
You can also Control Sequence Introducer ${CSI}#m
(optionally with multiple # separated by semicolon) to specify multiple color attributes. But the CSI tend to be temporary and various commands that produce their own colorized output overwrite the values you specify. E.g. setting the background color with ${CSI}43m
will set a yellow background but things like ls --color
will change it.
I have the following in my profile.
export CSI="$(echo -en "\x1B[")"
export OSC="$(echo -en "\x1B]")"
export ST="$(echo -en "\x1B\x5C")"
echo -n "${OSC}11;#404040${ST}" # set default background to medium grey
To give a background color to a command output in a terminal, you can typically use ANSI escape codes. These codes allow you to control various formatting aspects of text displayed in a terminal, including colors. Here's a general approach to achieve this:
Basic Method using ANSI Escape Codes
You can use the echo
command along with ANSI escape codes to print text with colored backgrounds. Here's an example:
echo -e "\e[41mHello, World!\e[0m"
In this example:
\e[41m
sets the background color to red (41 is the ANSI code for red background).\e[0m
resets the formatting so that the text after this will not have any special formatting.You can replace 41
with any other ANSI color code to change the background color. Here are some common ANSI color codes for backgrounds:
echo -e "\e[44mHello, World!\e[0m"
You can also combine background color with text color and other formatting options. For example, to print white text on a green background with bold formatting:
echo -e "\e[1;37;42mHello, World!\e[0m"
In this example:
1
turns on bold text.37
sets the text color to white.42
sets the background color to green.If you want to make your script more dynamic, you can use variables to store the ANSI escape codes for colors. For example:
RED='\e[41m'
RESET='\e[0m'
echo -e "${RED}Error:${RESET} Something went wrong."
This way, you can easily change the color throughout your script by modifying the variable values.
-e
option in echo
is used to enable interpretation of backslash escapes, allowing the escape codes to work.Keep in mind that using ANSI escape codes directly in scripts might make the output harder to read and maintain. For more complex formatting, you might consider using tools like tput
or libraries like colorama
for Python scripts.
The LLM above is fairly accurate, however the bang should be escaped, ie.
echo -e "\e[1;37;42mHello, World\!\e[0m"
not unless you SINGLE quote the string:
echo -e '\e[1;37;42mHello, World!\e[0m'
One of the reasons I always use QUOTES unless I need unquoted strings AND use SINGLE quotes unless I need DOUBLE quotes.