Shell output/Terminal color

change dispaly color on the terminal

The tput utility uses the terminfo database to make the values of terminal-depen-dent 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.

tput Color Capabilities:

  • tput setab [0-7] – Set a background color using ANSI escape
  • tput setb [0-7] – Set a background color
  • tput setaf [0-7] – Set a foreground color using ANSI escape
  • tput setf [0-7] – Set a foreground color

Color Code for tput:

  • 0 – Black
  • 1 – Red
  • 2 – Green
  • 3 – Yellow
  • 4 – Blue
  • 5 – Magenta
  • 6 – Cyan
  • 7 – White

bash script example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/bash

set -e

reset=$(tput sgr0)

green=$(tput setaf 2 )
blue=$(tput setaf 25)
red=$(tput setaf 1;tput bold)

info() {
printf "${blue}➜ %s${reset}\n" "$@"
}
error() {
printf "${red}× %s${reset}\n" "$@"
}
success() {
printf "${green}✔ %s${reset}\n" "$@"
}

infoWithTime(){
printf "${blue}➜ %s${reset}\n" "$@ | now: `date +'%F %T'`"
}

info "setup ,,info......"

error "haha,error..."

success "old color"

printf $(tput setaf 2;tput bold)'color show\n' $(tput sgr0)

printf $(tput setf 2;tput bold)'color show\n' $(tput sgr0)
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2019-2024 John Doe
  • Visitors: | Views:

请我喝杯咖啡吧~