In addition to the stty command, we can also use the tput command to change the parameters and functions of the terminal.
1 Function of tput command
The main functions of the tput command are: moving and changing the cursor, changing text display attributes (such as color, underline, bold), clearing specific areas of the screen, etc.
2 tput command format
tput [options] [parameters]
3 Set text color properties
3.1 tput setaf / setbf: set foreground color / background color
Command format:
- tput setab n: set background color, set text attributes background color
- tput setaf n: set the foreground color, set text attributes front color
Number n:
- 0 – Black, black
- 1 – Red
- 2 – Green
- 3 – Yellow, yellow
- 4 – Blue, blue
- 5 – Magenta, magenta
- 6 – Cyan
- 7 – White
Example: Set the foreground color to yellow (3) and the background color to magenta (5).
csdn @ edu bash ~ $ tput setaf 3 setab 5
csdn @ edu bash ~ $ tput setab 5
csdn @ edu bash ~ $
It seems that you cannot set the foreground color and background color at the same time in one command. You need to set them separately with two commands.
csdn @ edu bash ~ $ tput setaf 3; tput setab 5
csdn @ edu bash ~ $
3.2 tput rev: reverse the current color scheme
Rev comes from reverse.
csdn @ edu bash ~ $ tput setaf 3; tput setab 5
csdn @ edu bash ~ $ tput rev
csdn @ edu bash ~ $
4 Set text mode properties
Order | Function |
tput bold | Bold font |
tput dim | Turn on highlight mode, turn on half-bright mode |
tput savor | Add underline, start mode of underline, |
tput rmul | Cancel underline, remove mode of underline |
tput smso | standout mode, start mode of standout |
tput rmso | reverse mode of standout |
tput sgr0 | set global attributes return to 0, cancel all attributes |
csdn @ edu bash ~ $ tput setaf 3; tput setab 5
csdn @ edu bash ~ $ tput smso
csdn @ edu bash ~ $ tput rmso
csdn @ edu bash ~ $ tput smul
csdn @ edu bash ~ $ tput bold
csdn @ edu bash ~ $ tput rmul
csdn @ edu bash ~ $ tput dim
csdn @ edu bash ~ $ tput sgr0csdn@edu bash ~$
5 Set cursor attributes
Order | Function |
tput clear | clear screen |
tput sc | Save the current cursor position, save curosr position |
tput rc | Restore cursor position, restore cursor position |
tput cup row column | Move the cursor to the specified row and column, |
tput citizen | The cursor is invisible, cursor invisible |
tput cnorm | The cursor is visible, cursor morphal |
tpu init | clear format |
We will execute the following sequence of commands:
tput clear # 清屏
tput sc # 保存当前光标位置
tput cup 10 13 # 将光标移动到第10行第13列
echo -n Enter your password: # 提示用户输入密码
tput civis # 光标不可见
read p # 将用户输入的密码保存到变量p中
tput cnorm # 光标可见
tput rc # 恢复光标位置
echo your password: $p # 显示用户输入的密码
csdn @ edu bash ~ $ tput clear;tput sc;tput cup 10 13;echo -n Enter your password:;tput civis; read p; tput cnorm; tput rc; echo your password: $p
6 other functions
Order | Function |
tput lines | Display number of rows |
tpus cols | Display number of columns |
tput reset | Reset terminal settings |
tput longname | Display the long name of the current terminal type |
tput hs | Has status line |
csdn @ edu bash ~ $ tput lines
22
csdn @ edu bash ~ $ tput cols
132
csdn @ edu bash ~ $ tput longname
xterm terminal emulator (X Window System)csdn @ edu bash ~ $