Miscellaneous topics: terminal manipulation
In this part, we learn how to control what we display on terminal screen. For this purpose, we use tput
command. This command is available on most of Linux distributions. It is a command that allows us to manipulate the terminal by controlling the cursor on the terminal and the format of contents (e.g. color, bold, etc).
How to control the cursor position?
For controlling the cursor, we can use tput cup <row> <col>
command, it needs two arguments for horizontal and vertical positions. Check out below example.
# send cursor to position (7,10)
$ tput cup 7 10
Below, I put a brief list of useful tput
options for cursor control.
$ tput home # move to upper left corner (0,0)
$ tput cud1 # move the cursor down 1 line
$ tput cuu1 # move the cursor up 1 line
$ tput civis # make the curser invisible
$ tput cnorm # move the cursor to its normal state
How to change text effects?
For changing text effects, we can use tput
with text effect options, which displays every text after that command with the designed effect until we turn off the effect.
$ tput bold # make the text bold
$ tput smul # make the text underlined
$ tput rmul # turn off the text underlined option
$ tput smso # make the text to standout
$ tput rmso # turns off the standout mode
$ tput blink # make the text to blink
$ tput invis # make the text invisible
$ tput sgr0 # turn off all effects
$ setaf <val> # change the foreground color
$ setab <val> # change the background color
Checkout below example for learning how to use different text effects.
#!/bin/bash
echo "=== tput examples ==="; echo
echo "this is a regular text for comprison"; echo
# bold
tput bold
echo "\"tput bold\" makes the text bold."; echo
tput sgr0
# standout mode
tput smso
echo "\"tput smso\" makes the text to standout."; echo
tput sgr0
# underline
tput smul
echo "\"tput smul\" makes the text underlined."; echo
tput rmul
# blink
tput blink
echo "\"tput blink\" makes the text to blink."; echo
tput sgr0
# foreground color
tput setaf 40
echo "\"tput setaf\" changes the foreground color"; echo
tput sgr0
# background color
tput setab 140
echo "\"tput setab\" changes the background color"
tput sgr0
Below is the code for what I covered so far.
Another script code that asks user input and display it in the middle of screen. It also shows the exact position of the text.
Here I put a screen to make a clock on your screen. It has many lines of codes and I decided to write explanatory comments for different parts of the code to make it easier to understand the code. You need to install banner
before running the code.
$ sudo apt-get install sysvbanner
The idea credit of this fun clock belongs to this page.
References
For this part, I used the following references.
If you found this article helpful, share it with your friends and colleagues. If you have any other questions, you can find me on Linkedin or send me an email smohajer85@gmail.com.