I am writing a bash script. I would like to print a colored string to the terminal via the echo
command and have the color of the string change based on the background color of the terminal.
I currently am just printing the string in white like so: echo -e "$(tput setaf 7)my string here"
.
Is there a way to print the string in a different color if the terminal background color is also white? I would prefer not to set the background color myself to ensure that my string is visible.
Thanks in advance!
As far as I know, it's impossible to fetch the backgroud color of a terminal. Like John B said in the comments, it also depends on the terminal you are using.
If you want to force a specific background color you could use this command tput setab #
where # is a number between 0 and 7. Then you could reset the background color to the original color using the parameter op
like this : tput op
This link might also help (for the colors and more documentation on the color handling) : http://linux.about.com/library/cmd/blcmdl5_terminfo.htm