What does the read -p command do in bash scripting?
It displays a prompt message to the user and waits for input, storing the input in a variable.
Click to reveal answer
beginner
How do you store user input into a variable named name with a prompt "Enter your name:"?
Use read -p "Enter your name: " name to show the prompt and save input in name.
Click to reveal answer
intermediate
Can read -p be used without storing input in a variable?
Yes, in which case the input is stored in the special $REPLY variable.
Click to reveal answer
intermediate
What happens if you use read -p without a space after the prompt string?
The prompt message and user input appear together without space, which can confuse the user. Always add a space after the prompt.
Click to reveal answer
beginner
Write a simple bash script snippet that asks for a user's favorite color and then prints it.
read -p "What's your favorite color? " color
echo "Your favorite color is $color"
Click to reveal answer
What does the -p option do in the read command?
ADisplays a prompt before reading input
BPrints the input after reading
CPauses the script without input
DPrints a password prompt
✗ Incorrect
The -p option shows a prompt message before waiting for user input.
Which command correctly prompts for user input and stores it in variable age?
Aread age -p "Enter age:"
Bread -age "Enter age:"
Cread -p "Enter age:" age
Dread -p age "Enter age:"
✗ Incorrect
The correct syntax is read -p "prompt" variable.
What happens if you omit the variable name in read -p "Enter name:"?
AInput is discarded and lost
BInput is stored in $REPLY variable
CScript throws an error
DInput is stored in a default variable
✗ Incorrect
If no variable is given, input is stored in the special variable $REPLY.
Why should you add a space at the end of the prompt string in read -p?
ATo store input in uppercase
BTo make the script run faster
CTo avoid syntax errors
DTo separate prompt from user input visually
✗ Incorrect
Adding a space makes the prompt clear and separates it from the user input.
Which of these is a valid way to prompt for input and print it in bash?
Aread -p "Enter city:" city; echo "City: $city"
Becho "Enter city:"; read city; echo city
Cread city -p "Enter city:"; echo $city
Dread -p city "Enter city:"; echo $city
✗ Incorrect
Option A correctly uses read -p with variable and prints it.
Explain how to use read -p to ask a user for input and save it in a variable.
Think about how you show a question and keep the answer.
You got /4 concepts.
Describe common mistakes when using read -p and how to avoid them.
Focus on what can go wrong with the prompt and input storage.
You got /4 concepts.
Practice
(1/5)
1. What does the read -p command do in a bash script?
easy
A. It deletes a file specified by the user.
B. It prints a message without waiting for user input.
C. It runs a command silently without showing output.
D. It prompts the user with a message and stores their input in a variable.
Solution
Step 1: Understand the purpose of read -p
The read -p command shows a prompt message and waits for user input.
Step 2: Identify what happens with the input
The input entered by the user is saved into a variable for later use in the script.
Final Answer:
It prompts the user with a message and stores their input in a variable. -> Option D
Quick Check:
read -p = prompt and store input [OK]
Hint: Remember: read -p asks and saves input in one step [OK]
Common Mistakes:
Thinking read -p only prints text without input
Confusing read -p with commands that delete files
Assuming it runs commands silently
2. Which of the following is the correct syntax to prompt the user with "Enter your name:" and save it in variable name?
easy
A. read name -p "Enter your name:"
B. read -name "Enter your name:"
C. read -p "Enter your name:" name
D. read -prompt "Enter your name:" name
Solution
Step 1: Recall the correct order of read -p
The correct syntax is read -p "prompt message" variable.
Step 2: Match the syntax with options
read -p "Enter your name:" name matches the correct syntax exactly: read -p "Enter your name:" name.
Final Answer:
read -p "Enter your name:" name -> Option C
Quick Check:
Syntax = read -p "prompt" variable [OK]
Hint: Remember: -p comes before the prompt string [OK]
Common Mistakes:
Placing variable before -p
Using invalid flags like -name or -prompt
Missing quotes around the prompt message
3. What will be the output of this script if the user enters "Alice"?
read -p "Enter your name: " user
echo "Hello, $user!"
medium
A. Hello, Alice!
B. Hello, user!
C. Enter your name: Hello, !
D. Syntax error
Solution
Step 1: Understand the read -p command
The script prompts "Enter your name: " and waits for input, storing it in variable user.
Step 2: Substitute the variable in the echo command
If the user types "Alice", $user becomes "Alice", so the echo prints "Hello, Alice!".
Final Answer:
Hello, Alice! -> Option A
Quick Check:
User input = Alice, output = Hello, Alice! [OK]
Hint: Variable stores input; echo prints it with $variable [OK]
Common Mistakes:
Expecting the prompt text to appear in output
Confusing variable name with literal text
Assuming syntax error without checking code
4. Identify the error in this script snippet:
read -p Enter your age: age
echo "You are $age years old."
medium
A. The read command cannot use -p.
B. Missing quotes around the prompt message.
C. The echo command syntax is wrong.
D. Variable name cannot be age.
Solution
Step 1: Check the read -p syntax
The prompt message after -p must be enclosed in quotes to be treated as a single string.
Step 2: Identify the missing quotes
The script has read -p Enter your age: age without quotes around "Enter your age:", causing a syntax error.
Final Answer:
Missing quotes around the prompt message. -> Option B
Quick Check:
Prompt message needs quotes [OK]
Hint: Always quote prompt text after -p [OK]
Common Mistakes:
Not quoting prompt text with spaces
Thinking variable names are restricted
Misreading echo syntax as error
5. You want to write a script that asks the user for their favorite color and then prints "Your favorite color is COLOR" where COLOR is their input. Which script correctly does this using read -p?
hard
A. read -p "Favorite color?" color
echo "Your favorite color is $color"
B. read color -p "Favorite color?"
echo "Your favorite color is $color"
C. read -p Favorite color? color
echo "Your favorite color is $color"
D. read -prompt "Favorite color?" color
echo "Your favorite color is $color"
Solution
Step 1: Verify the correct read -p syntax
The prompt message must be quoted and -p must come before the prompt string and variable.
Step 2: Check each option
read -p "Favorite color?" color
echo "Your favorite color is $color" uses read -p "Favorite color?" color which is correct. Others have wrong order, missing quotes, or invalid flags.
Final Answer:
read -p "Favorite color?" color
echo "Your favorite color is $color" -> Option A
Quick Check:
Correct syntax with quotes and variable last [OK]
Hint: Use read -p "prompt" variable with quotes [OK]
Common Mistakes:
Placing variable before -p
Not quoting prompt text
Using invalid flags like -prompt
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.