1. Overview

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.

2. Using the pv Command

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:

  • echo “Emulating typing effect in the terminal” – the echo command is meant to print the line of text inside the double quotes to the terminal
  • | – the pipe symbol redirects the output of echo as input to the pv command
  • pv -qL 10 – this command controls the rate at which text is displayed

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.

3. Using Loops

In this section, we combine loops with commands like echo, sleep, and read.

3.1. Using a for Loop

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:

  • text=”Emulating typing effect in the terminal” – this line initializes a variable text with the string Emulating typing effect in the terminal
  • delay=0.1 – initializes another variable delay with the value 0.1 to represent the delay between each character during printing
  • for ((i = 0; i < ${#text}; i++)); do – the loop variable i initializes at 0 and increases by 1 during each iteration until it reaches the length of the text
  • echo -n “${text:$i:1}” – this line inside the loop prints a character from the text variable at position i; -n option instructs echo not to add a newline character after printing each one
  • sleep $delay – instructs the script to sleep for the time specified in the delay variable creating a delay when printing each character
  • done – specifies the end of the for loop
  • echo – this line prints a newline character after the typing effect is complete ensuring the next prompt appears on a new line

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.

3.2. Using a while Loop

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:

  • while IFS= read -r -n1 char; do – this line reads characters one at a time from standard input, preserving whitespace
  • printf ‘%s’ “$char” – this line prints the character stored in the char variable
  • sleep 0.1 – adds a delay of 0.1 seconds after printing each character
  • done – this is the end of the loop
  • <<< “$text” – redirects the content of the text variable to the standard input of the while loop
  • echo – makes sure the cursor moves to the next line after text typing is complete

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.

4. Conclusion

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.