📖 Scenario: You want to create a simple script that asks a user for their name and age, then uses that information to display a greeting message.
🎯 Goal: Build a Bash script that uses the read command to get user input and then prints a message using that input.
📋 What You'll Learn
Use the read command to get user input
Store the input in variables named name and age
Print a greeting message using the input values
💡 Why This Matters
🌍 Real World
Scripts often need to ask users for information like names, passwords, or choices. The <code>read</code> command is the basic way to get this input in Bash.
💼 Career
Knowing how to get user input in scripts is essential for system administrators, DevOps engineers, and anyone automating tasks with Bash.
Progress0 / 4 steps
1
Create variables to hold user input
Write a Bash script that uses the read command to get the user's name and store it in a variable called name.
Bash Scripting
Hint
Use read -p "Enter your name: " name to prompt and store input.
2
Add input for user's age
Add a read command to get the user's age and store it in a variable called age.
Bash Scripting
Hint
Use read -p "Enter your age: " age to prompt and store input.
3
Create a greeting message using the input
Write a echo command that prints: Hello, [name]! You are [age] years old. using the variables name and age.
Bash Scripting
Hint
Use echo "Hello, $name! You are $age years old." to print the message.
4
Run the script and display the output
Run the script and show the output after entering John as the name and 30 as the age. The output should be: Hello, John! You are 30 years old.
Bash Scripting
Hint
Type John when prompted for name and 30 when prompted for age.
Practice
(1/5)
1. What does the read command do in a bash script?
easy
A. It runs another script.
B. It prints text to the screen.
C. It waits for user input and stores it in a variable.
D. It deletes files.
Solution
Step 1: Understand the purpose of read
The read command pauses the script and waits for the user to type something.
Step 2: Identify what happens with the input
The input typed by the user is saved into a variable for later use in the script.
Final Answer:
It waits for user input and stores it in a variable. -> Option C
Quick Check:
read = wait for input and save [OK]
Hint: Remember: read = get input from user [OK]
Common Mistakes:
Thinking read prints output
Confusing read with echo
Assuming read runs commands
2. Which of the following is the correct syntax to read a user's name into a variable called username?
easy
A. read > username
B. read -p username
C. read $username
D. read username
Solution
Step 1: Recall basic read syntax
The correct syntax to store input is read variable_name without any special characters.
Step 2: Check each option
read username uses read username, which correctly stores input in username. Others misuse flags or symbols.
Final Answer:
read username -> Option D
Quick Check:
read variable_name = correct syntax [OK]
Hint: Use read followed by variable name only [OK]
Common Mistakes:
Adding $ before variable in read
Using > instead of variable name
Misusing -p without prompt text
3. What will be the output of this script?
echo "Enter your age:" read age echo "You are $age years old."
medium
A. You are [user input] years old.
B. You are years old.
C. You are $age years old.
D. Syntax error
Solution
Step 1: Understand script flow
The script prints a prompt, waits for user input saved in age, then prints a message using that input.
Step 2: Predict output after input
If user types 25, output will be "You are 25 years old." showing the input value.
Final Answer:
You are [user input] years old. -> Option A
Quick Check:
read stores input, echo prints it [OK]
Hint: Input replaces variable in echo output [OK]
Common Mistakes:
Expecting literal $age printed
Thinking read does not save input
Assuming syntax error without reason
4. Identify the error in this script snippet:
read -p "Enter your city: " city echo "City is $city"
medium
A. The script is correct and will work as expected.
B. The variable name should not be after the prompt.
C. The -p option is not valid with read.
D. Quotes around prompt cause syntax error.
Solution
Step 1: Check usage of read with -p
The -p option allows showing a prompt before reading input, which is valid in bash.
Step 2: Verify variable placement and quotes
The variable city is correctly placed after the prompt, and quotes around prompt text are correct.
Final Answer:
The script is correct and will work as expected. -> Option A
Quick Check:
read -p "prompt" variable = valid [OK]
Hint: read -p "prompt" variable is correct syntax [OK]
Common Mistakes:
Thinking -p is invalid
Misplacing variable before prompt
Removing quotes around prompt
5. You want to read two values, first and last, from one line of user input separated by space. Which command correctly does this?
hard
A. read -p first last
B. read first last
C. read first; read last
D. read -a first last
Solution
Step 1: Understand reading multiple variables
The read command can take multiple variable names to split input by spaces.
Step 2: Analyze each option
read first last correctly uses read first last to read two words into two variables. read -p first last misuses -p. read first; read last reads twice separately. read -a first last uses -a incorrectly here.
Final Answer:
read first last -> Option B
Quick Check:
read var1 var2 = split input by space [OK]
Hint: Use read with multiple variables to split input [OK]
Common Mistakes:
Using multiple read commands instead of one
Misusing -p option for multiple variables
Using -a without array handling
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.