Using the `read` command (with examples)

Using the `read` command (with examples)

The read command is a shell builtin that allows you to retrieve data from stdin (standard input). It is commonly used to read user input from the keyboard and store it in a variable. However, it can also be used in various other ways, such as reading from a file or performing an action on each line of input.

In this article, we will explore three different use cases of the read command, along with their corresponding code examples, motivations, explanations, and example outputs.

1: Storing data typed from the keyboard

This use case demonstrates the basic functionality of the read command, which is to read user input from the keyboard and store it in a variable. By default, the read command will stop reading input when you press the Enter key.

Code example:

read variable

Motivation:

The motivation behind using this example is to allow the user to input a value and store it in a variable for further processing in the script.

Explanation:

  • The read command is used to read input from the keyboard.
  • The variable is the name of the variable in which the user input will be stored.

Example output:

If the user types “hello” and presses Enter, the value “hello” will be stored in the variable variable.

2: Preventing backslash (\) from acting as an escape character

In some cases, you may want to read input from the keyboard without interpreting any backslashes as escape characters. This is useful when you want to preserve the literal value of the input, including any backslashes.

Code example:

read -r variable

Motivation:

The motivation behind using this example is to retrieve user input from the keyboard, while preserving any backslashes in the input string.

Explanation:

  • The -r option is used to prevent backslashes from being interpreted as escape characters.

Example output:

If the user types “Hello\World” and presses Enter, the value “Hello\World” will be stored in the variable variable.

3: Reading stdin and performing an action on every line

In this use case, we will demonstrate how the read command can be used in combination with a loop to read input from stdin and perform an action on each line. This is useful when you have a file or command output that contains multiple lines of data, and you want to process each line individually.

Code example:

while read line; do
  echo "$line"
done

Motivation:

The motivation behind using this example is to read input from stdin line by line and perform an action on each line.

Explanation:

  • The while loop is used to iterate over each line of input.
  • The read command is used to read each line of input and store it in the variable line.
  • The echo command is used to display each line of input.

Example output:

If the input contains the following lines:

Hello
World
Goodbye

The output will be:

Hello
World
Goodbye

Each line of input is displayed separately, thanks to the echo command within the loop.

Related Posts

Vue CLI: Introduction to the Multi-purpose CLI for Vue.js (with examples)

Vue CLI: Introduction to the Multi-purpose CLI for Vue.js (with examples)

Vue CLI is a command-line interface tool that provides a set of helpful commands for developing Vue.

Read More
How to use the command 'ical' (with examples)

How to use the command 'ical' (with examples)

The ‘ical’ command is a Hirji/Islamic calendar and converter for the terminal.

Read More
Exploring the aws-google-auth Command-line Tool (with examples)

Exploring the aws-google-auth Command-line Tool (with examples)

Introduction The aws-google-auth command-line tool allows users to acquire temporary AWS credentials, known as Security Token Service (STS) credentials, using Google Apps as a federated Single Sign-On (SSO) provider.

Read More