2

On Windows OS, I can type just color a or color b or color c etc. to change the font color in the terminal temporarily.

Is there a command to do this in bash terminal?

I don't want to make permanent changes, just for the open session is enough.

6

You're referring to a command that expects values 0-f (i.e., 0 through 15 decimal). The analogous command would use tput:

tput setaf 10

("a" is hexdecimal for 10), for the foreground color and

tput setab 10

for the background color. Those are separate terminfo capabilities.

That example assumes your terminal description has at least 16 colors. If it has only 8, tput will not notice, but the result will be unsatisfactory. You can see this by

tput colors

By default, tput uses the current value of $TERM to select a terminal description. You can override this with the -T option. For example, if you are using xterm (as opposed to some other terminal which sets TERM to "xterm"), you could use the xterm-16color terminal description as shown in this screenshot:

enter image description here

(You can probably see that the tput on line 15 had no useful effect, which is expected since 10 is outside the range of 8 colors).

The terminal description, whether xterm or xterm-16color (or xterm-256color) tells tput how to make an escape sequence which can be written to the terminal. Your bash prompt may send escape sequences as well, setting or resetting colors. If you called tput and echoed text within a shell script, you might see the colored text while the same commands (separated by bash prompts) might not behave the same way. Also, some programs (such as ls) set/reset colors. There is only one terminal's color-state to share with these different applications; terminals don't automatically switch between them.

Testing a shell script is more predictable than relying on details of your shell prompt. Here is an example

#!/bin/sh
case $TERM in
xterm*)
        TERM=xterm-256color
        ;;
linux*)
        TERM=linux-16color
        ;;
esac
export TERM
for p in $(seq 0 15)
do
        tput setab $p
        for q in $(seq 0 15)
        do
                tput setaf $q
                printf '%x%x' $p $q
        done
        tput sgr0
        printf '\n'
done

and a screenshot in xterm:

enter image description here

| improve this answer | |
  • actually, I have tried these ones but either they did not work or I could not understand exactly. – muyustan Apr 8 at 23:23
  • 2
    @muyustan or something else might be interfering (say, you have a coloured prompt which resets the colours) – muru Apr 9 at 2:47
  • This is not the analogous command, because this is not what the color command does. To do what the color command does, one would need a DECCARA that could change colours (as some terminal emulators support as an extension), and a way to describe that via terminfo. The color command both sets the current pen/paper colours and changes the colours of all existing cells in the screen buffer. – JdeBP Apr 9 at 13:02
  • @JdeBP actually, if it would be able to change the lines which comes next, it would be enough. However, even that is not satisfied in my situation. I am adding what my terminal looks like when I use the commands in this answer. See my comment below – muyustan Apr 9 at 13:50
  • I am adding how my terminal(default of Linux Mint 19.03) responds: ibb.co/g98D2j5 -- It looks like, setaf does not do anything and setab does not do exactly what your example shows. – muyustan Apr 9 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.