Colors in bash linux

Colors In Bash Scripts

Colors In Bash Scripts

Using colors in bash scripts can be very useful. If you’ve written your bash script with menus and prompts, it’s a good idea to use ansi color codes to make your outputs more readable to the user. In this tutorial I’ll cover some ways that you can use colors in your bash scripts for a more GUI like experience. Ansi color codes always start with these characters “\e[” followed by the numbers that determine the color you want. There are color codes for the foreground(text) and background. There are also color codes for bold, underlines, and high intensity colors. Colors in bash scripts can be used to distinguish text. For example, you can make error outputs in red text while prompts with instructions in green text. Use bold and underlines to emphasize words and phrases. Background colors can be used for section separation. There’s many ways to put color codes into good use in your bash scripts.

Style Your Bash Script With Ansi Colors

Lets start off by creating a file called “test.sh”. Now copy the code below to the file and give it execute permissions and then run the file in your terminal. test.sh:

#!/bin/bash # Ansi color code variables red="\e[0;91m" blue="\e[0;94m" expand_bg="\e[K" blue_bg="\e[0;104m$expand_bg>" red_bg="\e[0;101m$expand_bg>" green_bg="\e[0;102m$expand_bg>" green="\e[0;92m" white="\e[0;97m" bold="\e[1m" uline="\e[4m" reset="\e[0m" # horizontally expanded backgrounds echo -e "$blue_bg>$reset>" echo -e "$red_bg>$reset>" echo -e "$green_bg>$reset>" echo "" # colored text echo -e "$red>Hello World!$reset>" echo -e "$blue>Hello World!$reset>" echo -e "$green>Hello World!$reset>" echo -e "$white>Hello World!$reset>" echo "" # bold colored text echo -e "$red>$bold>Hello World!$reset>" echo -e "$blue>$bold>Hello World!$reset>" echo -e "$green>$bold>Hello World!$reset>" echo -e "$white>$bold>Hello World!$reset>" echo "" # underlined colored text echo -e "$red>$uline>Hello World!$reset>" echo -e "$blue>$uline>Hello World!$reset>" echo -e "$green>$uline>Hello World!$reset>" echo -e "$white>$uline>Hello World!$reset>" echo "" # ansi across multiple lines echo -e "$green>This is a sentence across" echo "three lines to show how an ansi color" echo -e "works across multiple lines with echo.$reset>" echo "" # combining ansi in one line echo -e "$red>This sentence $green>displays $blue>ansi code used in $white>$bold>combination.$reset>"

Lets break things down a bit… As you can see, we used a few colors and also applied underlines, bold text, and backgrounds. It’s a good idea to place your color codes in global variables at the top of your script to keep your code neat. Notice how we always use the echo “-e” flag on lines with color codes. This tells echo to enable interpretation of escapes so that the color codes work. We also place the color code variables in curly brackets “$<>” to separate it from normal text.

echo -e "$red>Hello World!$reset>"

For paragraphs or multi line outputs you do not need the echo “-e” for every line as displayed below.

echo -e "$green>This is a sentence across" echo "three lines to show how an ansi color" echo -e "works across multiple lines with echo.$reset>"

We also use a “reset” variable containing “\e[0m”. This is to stop the ansi code in use, otherwise it will continue running into further output as you saw in the example above with the multi lines.

Ansi Codes List For Bash

# Regular Colors | Value | Color | | -------- | ------ | | \e[0;30m | Black | | \e[0;31m | Red | | \e[0;32m | Green | | \e[0;33m | Yellow | | \e[0;34m | Blue | | \e[0;35m | Purple | | \e[0;36m | Cyan | | \e[0;37m | White | # Bold | Value | Color | | -------- | -------- | | \e[1;30m | Black | | \e[1;31m | Red | | \e[1;32m | Green | | \e[1;33m | Yellow | | \e[1;34m | Blue | | \e[1;35m | Purple | | \e[1;36m | Cyan | | \e[1;37m | White | | \e[1m | No Color | # Underline | Value | Color | | -------- | -------- | | \e[4;30m | Black | | \e[4;31m | Red | | \e[4;32m | Green | | \e[4;33m | Yellow | | \e[4;34m | Blue | | \e[4;35m | Purple | | \e[4;36m | Cyan | | \e[4;37m | White | | \e[4m | No Color | # Background | Value | Color | | ------ | ------ | | \e[40m | Black | | \e[41m | Red | | \e[42m | Green | | \e[43m | Yellow | | \e[44m | Blue | | \e[45m | Purple | | \e[46m | Cyan | | \e[47m | White | # Expand Background Horizontally | Value | Color | | ----- | -------- | | \e[K | No Color | # High Intensty | Value | Color | | -------- | ------ | | \e[0;90m | Black | | \e[0;91m | Red | | \e[0;92m | Green | | \e[0;93m | Yellow | | \e[0;94m | Blue | | \e[0;95m | Purple | | \e[0;96m | Cyan | | \e[0;97m | White | # Bold High Intensty | Value | Color | | -------- | ------ | | \e[1;90m | Black | | \e[1;91m | Red | | \e[1;92m | Green | | \e[1;93m | Yellow | | \e[1;94m | Blue | | \e[1;95m | Purple | | \e[1;96m | Cyan | | \e[1;97m | White | # High Intensty backgrounds | Value | Color | | --------- | ------ | | \e[0;100m | Black | | \e[0;101m | Red | | \e[0;102m | Green | | \e[0;103m | Yellow | | \e[0;104m | Blue | | \e[0;105m | Purple | | \e[0;106m | Cyan | | \e[0;107m | White | # Reset | Value | Color | | ----- | ------ | | \e[0m | Reset |

Conclusion

Источник

How do I get a colored bash?

colored-bash

How can I get the bash to look colored like this?

@kva Answering your own question at the same time as posting is encouraged across the Stack Exchange network.

Related: askubuntu.com/questions/123268/…. It explains how individual parts of the prompt can be coloured differently.

5 Answers 5

Open ~/.bashrc in text editor and uncomment line:

save then execute source ~/.bashrc

Here is what mine looks like after your method: !2016-10-25 16:12:15.png Not exactly as in the question.

Is force_color_prompt=yes the intended way of enabling colors? To me forcing sounds like a workaround.

I hope I didn’t sound disrespectful with my comments, I’m just trying to understand how it was meant to work. For example, above those lines you mention, there’s a different way of enabling colors, xterm-color|*-256color) color_prompt=yes;; Which makes me think that the colors could automatically enable if you had correct value in TERM variable.

I came up with this solution:

  • open ~/.bashrc in an editor
  • copy this and add it at the end of .bashrc file:
PS1='\[\033[1;36m\]\u\[\033[1;31m\]@\[\033[1;32m\]\h:\[\033[1;35m\]\w\[\033[1;31m\]\$\[\033[0m\] ' 

For a full list of available colors and further options look up these links:

A version that is a bit more ‘general’ — should work with a varied environment:
(depends on terminfo)

Insert this in your $HOME/.bashrc :

function fgtab < echo "tput setf/setb - Foreground/Background table" for f in ; do for b in ; do echo -en "$(tput setf $f)$(tput setb $b) $f/$b " done echo -e "$(tput sgr 0)" done > # The prompt in a somewhat Terminal -type independent manner: cname="$(tput setf 3)" csgn="$(tput setf 4)" chost="$(tput setf 2)" cw="$(tput setf 6)" crst="$(tput sgr 0)" PS1="\[$\]\u\[$\]@\[$\]\h:\[$\]\w\[$\]\$\[$\] " 

Then execute source ~/.bashrc .

After that, fgtab will display a color table with numbers. Those numbers are for tput setf n and tput setb n where ‘n’ is the number, ‘f’ stands for ‘foreground’ and ‘b’ stands for ‘background’ color.

tput sgr 0 will reset foreground and background colors to default.

And as you can see, changing the colors used for the prompt becomes really easy (just edit the same number in $HOME/.bashrc as you wish).

Add an $(tput setb n) in $cname if you wish to have ALL of the prompt with background n.

Источник

Читайте также:  Linux монтирование сетевой папки centos
Оцените статью
Adblock
detector