0
0
Bash Scriptingscripting~5 mins

Prompting with read -p in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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
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:"
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
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
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
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.