0
0
Bash Scriptingscripting~15 mins

Reading into multiple variables in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Reading into Multiple Variables in Bash
📖 Scenario: You work in a small office where you receive daily reports as single lines of text. Each line contains a person's first name, last name, and age separated by spaces. You want to write a simple script to read these details into separate variables for easy use later.
🎯 Goal: Build a Bash script that reads a single line of input containing a first name, last name, and age, and stores each piece of information into its own variable.
📋 What You'll Learn
Create a variable to hold the input line.
Create three variables: first_name, last_name, and age.
Use the read command to split the input line into these three variables.
Print the variables to confirm the values were stored correctly.
💡 Why This Matters
🌍 Real World
This technique helps when processing lines of text data, such as logs or reports, where each line contains multiple pieces of information separated by spaces.
💼 Career
Many automation and scripting tasks require parsing input into variables for further processing, making this a fundamental skill for system administrators and DevOps engineers.
Progress0 / 4 steps
1
Set up the input line
Create a variable called input_line and set it to the string "John Doe 28".
Bash Scripting
Need a hint?

Use the syntax variable_name="value" to assign a string to a variable in Bash.

2
Prepare variables for reading
Create three empty variables called first_name, last_name, and age.
Bash Scripting
Need a hint?

Assign empty strings to variables using variable_name="".

3
Read the input line into variables
Use the read command with variables first_name, last_name, and age to split the contents of input_line into these variables.
Bash Scripting
Need a hint?

Use read var1 var2 var3 <<< "$input_line" to split the string into variables.

4
Print the variables
Print the variables first_name, last_name, and age each on its own line using echo.
Bash Scripting
Need a hint?

Use echo "$variable" to print each variable on its own line.