linux shell 字体颜色样板,Linux_Shell 输出带颜色的字体(一) 利用tput

原文出自stack-overflow

Usage

Specific tput sub-commands are discussed later.

Direct

Call tput as part of a sequence of commands:

tput setaf1;echo"this is red text"

Use ; instead of && so if tput errors the text still shows.

Shell variables

Another option is to use shell variables:

red=`tput setaf 1`green=`tput setaf 2`reset=`tput sgr0`echo"${red}red text ${green}green text${reset}"

tput produces character sequences that are interpreted by the terminal as having a special meaning. They will not be shown themselves. Note that they can still be saved into files or processed as input by programs other than the terminal.

Command substitution

It may be more convenient to insert tput's output directly into your echo strings using command substitution:

echo"$(tput setaf 1)Red text $(tput setab 7)and white background$(tput sgr 0)"

Example

The above command produces this on Ubuntu:

0818b9ca8b590ca3270a3433284dd417.png

Foreground & background colour commands

tput setab[1-7]# Set the background colour using ANSI escapetput setaf[1-7]# Set the foreground colour using ANSI escape

Colours are as follows:

Num Colour #define R G B 0 black COLOR_BLACK 0,0,0 1 red COLOR_RED 1,0,0 2 green COLOR_GREEN 0,1,0 3 yellow COLOR_YELLOW 1,1,0 4 blue COLOR_BLUE 0,0,1 5 magenta COLOR_MAGENTA 1,0,1 6 cyan COLOR_CYAN 0,1,1 7 white COLOR_WHITE 1,1,1

There are also non-ANSI versions of the colour setting functions (setb instead of setab, and setfinstead of setaf) which use different numbers, not given here.

Text mode commands

tput bold# Select bold modetput dim# Select dim (half-bright) modetput smul# Enable underline modetput rmul# Disable underline modetput rev# Turn on reverse video modetput smso# Enter standout (bold) modetput rmso# Exit standout mode

Cursor movement commands

tput cup Y X# Move cursor to screen postion X,Y (top left is 0,0)tput cuf N# Move N characters forward (right)tput cub N# Move N characters back (left)tput cuu N# Move N lines uptput ll# Move to last line, first column (if no cup)tput sc# Save the cursor positiontput rc# Restore the cursor positiontput lines# Output the number of lines of the terminaltput cols# Output the number of columns of the terminal

Clear and insert commands

tput ech N# Erase N characterstput clear# Clear screen and move the cursor to 0,0tput el1# Clear to beginning of linetput el# Clear to end of linetput ed# Clear to end of screentput ich N# Insert N characters (moves rest of line forward!)tput il N# Insert N lines

Other commands

tput sgr0# Reset text format to the terminal's defaulttput bel# Play a bell

With compiz wobbly windows, the bel command makes the terminal wobble for a second to draw the user's attention.

Scripts

tput accepts scripts containing one command per line, which are executed in order before tputexits.

Avoid temporary files by echoing a multiline string and piping it:

echo-e"setf 7\nsetb 1"|tput-S# set fg white and bg red

See also

See man 5 terminfo for the complete list of commands and more details on these options. (The corresponding tput command is listed in the Cap-name column of the huge table that starts at line 81.)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值