Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: April 21, 2024
Displaying text in the Linux terminal is straightforward. However, emulating a typing effect when displaying the text can enhance the user experience. For instance, emulating a typing effect makes the text output more engaging by providing visual feedback. This imitates human interaction with the Linux machine.
In this tutorial, we’ll explore various approaches to emulate a typing effect when displaying text in the terminal.
Before proceeding, let’s understand the basics of the typing effect. The typing effect involves displaying text characters one after the other as if we’re typing in real-time. This effect is useful for demonstrations, tutorials, or for providing an interactive experience.
The pv command is a command-line tool for monitoring the progress of data transfer through a pipeline. First, let’s ensure that pv is installed on our system. If not, we can use a package manager to install it. For example, we can use apt-get on Ubuntu and Debian-based Linux systems:
$ sudo apt-get install pv
In this example, we can see it’s easy to install the pv command.
Once the pv command is installed, let’s emulate the typing effect:
$ echo "Emulating typing effect in the terminal" | pv -qL 10
Here’s a breakdown of the above command:
In pv -qL 10, the -q option discards unnecessary information like the progress bars so that only the content appears. Then -L 10 specifies the data transfer rate through the pipeline, measured in bytes per second. In this scenario, each character is treated as a byte, therefore -L 10 means that 10 characters pass through the pipeline per second.
In this section, we combine loops with commands like echo, sleep, and read.
Here, we utilize a for loop to iterate through the intended text string and print each character:
#!/bin/bash
text="Emulating typing effect in the terminal"
delay=0.1
for ((i = 0; i < ${#text}; i++)); do
echo -n "${text:$i:1}"
sleep $delay
done
echo
Let’s analyze the content of the script file:
The above script prints each text character with a 0.1-second delay. So, to control the typing speed, we can modify the delay variable.
Alternatively, we can utilize a while loop in combination with the read and sleep commands:
#!/bin/bash
text="Emulating typing effect in the terminal"
while IFS= read -r -n1 char; do
printf "%s" "$char"
sleep 0.1
done <<< "$text"
echo
Let’s analyze the script contents:
In while IFS= read -r -n1 char; do, IFS= sets the Internal Field Separator as empty to preserve leading and trailing whitespace. Meanwhile, the read -r -n1 char command reads one character (-n1) at a time from the input string and stores each character in the char variable. The -r option prevents backslashes from being interpreted as escape characters.
In this article, we explored how to emulate the typing effect in Linux.
Emulating a typing effect when displaying text in the terminal enhances user experience by providing an illusion of typing in real time. Whether for tutorials or other purposes, the approaches we discussed offer simple but effective ways to achieve this effect in Linux.