Click on each book below to review & buy on Amazon.
As an Amazon Associate, I earn from qualifying purchases.
CompTIA Linux+ XK0-005 - 3.1 - Shell Script Elements - Built-in Commands: read
In shell scripting, the read
command is a built-in command that allows you to read input from the user or from a file. It is a versatile command that enables interactive script execution and user interaction. Understanding how to use the read
command is essential for creating dynamic and user-friendly shell scripts. In this guide, we will explore the read
command and its various applications.
Using the read Command
The basic syntax of the read
command is as follows:
read [options] variable
options
: Additional options that modify the behavior of theread
command. We will cover some commonly used options later in this guide.variable
: The name of the variable that will store the input value read by theread
command.
Reading User Input
One common use case for the read
command is to prompt the user for input during script execution. Here's an example:
#!/bin/bash
echo "Enter your name:"
read name
echo "Hello, $name! Welcome to the script."
In this example, the read
command prompts the user to enter their name, and the value is stored in the name
variable. The script then greets the user using the entered name.
Handling Multiple Inputs
You can use multiple read
commands to prompt the user for different inputs. Here's an example:
#!/bin/bash
read -p "Enter your name: " name
read -p "Enter your age: " age
echo "Hello, $name! You are $age years old."
In this script, we use the -p
option to provide a prompt message for each input. The values entered by the user are stored in the respective variables (name
and age
), and the script displays a personalized message using these values.
Reading Input from a File
The read
command can also read input from a file. This is useful when you want to process data from a file within a script. Here's an example:
#!/bin/bash
filename="data.txt"
while read -r line
do
echo "Processing: $line"
# Add your processing logic here
done < "$filename"
In this example, the read
command reads each line from the data.txt
file and stores it in the line
variable. You can then perform any required processing on each line within the loop.
Additional Options
The read
command provides various options to customize its behavior. Here are a few commonly used options:
-p
: Specifies a prompt message to be displayed before reading input.-s
: Makes the input silent, without displaying it on the screen (useful for password input).-t
: Sets a timeout for input, after which theread
command exits if no input is provided.-a
: Reads input into an array variable.
Consult the documentation or man
page for the read
command to explore more options and their usage.
Conclusion
The read
command is a versatile tool in shell scripting that allows you to interact with users, read input from files, and create dynamic and interactive scripts. By incorporating the read
command into your scripts, you can prompt users for input, process data from files, and create engaging user experiences.
Support DTV Linux
Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.
NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.