0

I would like to modify a i3Blocks script (utility blocks for i3 WM enviroment) that print out the bandwidth in/out .. In particular I would like to change the color of this instruction output:

echo -n " $INLABEL"

using color #9fbc00 how can I do that ? thanks

2
1

Short answer you can't because true 24 bit colours (rgb) are not part of an accepted standard and it will be a hit or miss whether a terminal supports it or not. You should instead use a predefined colour from the terminal's pallette. ANSI 3/4 bit colours

That being said, some terminals do support true 24bit colours, and most of them use this format: \033[48;2;r;g;bm the 4 in 48 sets the colour as the background, it can be changed to 3 which defines the colour as foreground. (more info)

The r g and b letters represent an 8 bit number in decimal so printf "\033[38;2;159;188;0mhello would print a hello in the colour you wanted (#9fbc00) but it wouldn't reset once the command was executed so you need to tell the terminal to go back to the original colour. To do that, you need to use another escape sequence. \033[0m

Now knowing that, a bug free way to print Hello world! in #9fbc00 would be

printf "\033[38;2;159;188;0mHello world!\033[0m\n".

ps: echo doesn't like escape codes, use printf if you want to format stuff

3
  • thanks ! this works like a charm in termianal .. unfortunately does not works for i3block script .. in the bar appearance I get all the coloring code !!! but your answer is appropriate since I 've asked for BASH :) – Drudox lebowsky Apr 30 '20 at 19:42
  • @Drudoxlebowsky I haven't used i3 block in a while but [time] color=#ffcc00 command=date '+%A %d-%B %H:%M' interval=5 should be the template (one line per variable) i cant get the format to work in a comment though – lor_louis Apr 30 '20 at 19:45
  • yes but this change the colour of the full block and not gust of the label – Drudox lebowsky May 1 '20 at 16:44

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.