0
0
Bash Scriptingscripting~20 mins

Prompting with read -p in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prompting Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this script?
Consider this bash script snippet that prompts the user for input and then echoes it back:
Bash Scripting
read -p "Enter your name: " name
 echo "Hello, $name!"
AIt prints nothing because read -p does not work in bash.
BIt prints: Enter your name: Hello, followed by the entered name.
CIt prints: Hello, followed by the entered name.
DIt prints: Hello, $name! literally without replacing $name.
Attempts:
2 left
💡 Hint
The read -p option shows a prompt and stores input in a variable.
💻 Command Output
intermediate
2:00remaining
What happens if you run this script?
This script uses read -p but tries to read into two variables:
Bash Scripting
read -p "Enter first and last name: " first last
 echo "First: $first"
 echo "Last: $last"
AIt stores the entire input in 'first' and leaves 'last' empty.
BIt stores the first character in 'first' and the rest in 'last'.
CIt causes a syntax error because read -p cannot read into multiple variables.
DIt stores the first word in 'first' and the second word in 'last', then prints both.
Attempts:
2 left
💡 Hint
read can split input by spaces into multiple variables.
📝 Syntax
advanced
2:00remaining
Which option correctly uses read -p to prompt and store input?
Select the valid bash command that prompts the user and stores input in variable 'age':
Aread -p "Enter your age: " age
Bread age -p "Enter your age: "
Cread "Enter your age: " -p age
Dread -prompt "Enter your age: " age
Attempts:
2 left
💡 Hint
The -p option must come immediately after read.
🔧 Debug
advanced
2:00remaining
Why does this script not show the prompt?
This script tries to prompt the user but the prompt does not appear:
Bash Scripting
read "Enter your city: " city
 echo "City: $city"
ABecause read needs the -p option to show a prompt.
BBecause the variable name must come before the prompt string.
CBecause the prompt string must be in single quotes, not double quotes.
DBecause read cannot store input in a variable named 'city'.
Attempts:
2 left
💡 Hint
Check the syntax for showing a prompt with read.
🚀 Application
expert
3:00remaining
How to securely prompt for a password without echoing input?
You want to prompt the user for a password using read -p but hide the input as they type. Which command achieves this?
A
read -p "Enter password: " -s password
 echo "Password length: ${#password}"
B
read -sp "Enter password: " password
 echo "Password length: ${#password}"
C
read -p "Enter password: " password
 stty -echo
 echo "Password length: ${#password}"
D
read -p "Enter password: " password
 echo "Password: $password"
Attempts:
2 left
💡 Hint
Use -s option with read to hide input.