Bird
Raised Fist0
Bash Scriptingscripting~20 mins

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

Choose your learning style10 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.

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

  1. Step 1: Understand the purpose of read -p

    The read -p command shows a prompt message and waits for user input.
  2. 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.
  3. Final Answer:

    It prompts the user with a message and stores their input in a variable. -> Option D
  4. 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

  1. Step 1: Recall the correct order of read -p

    The correct syntax is read -p "prompt message" variable.
  2. Step 2: Match the syntax with options

    read -p "Enter your name:" name matches the correct syntax exactly: read -p "Enter your name:" name.
  3. Final Answer:

    read -p "Enter your name:" name -> Option C
  4. 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

  1. Step 1: Understand the read -p command

    The script prompts "Enter your name: " and waits for input, storing it in variable user.
  2. Step 2: Substitute the variable in the echo command

    If the user types "Alice", $user becomes "Alice", so the echo prints "Hello, Alice!".
  3. Final Answer:

    Hello, Alice! -> Option A
  4. 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

  1. Step 1: Check the read -p syntax

    The prompt message after -p must be enclosed in quotes to be treated as a single string.
  2. 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.
  3. Final Answer:

    Missing quotes around the prompt message. -> Option B
  4. 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

  1. Step 1: Verify the correct read -p syntax

    The prompt message must be quoted and -p must come before the prompt string and variable.
  2. 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.
  3. Final Answer:

    read -p "Favorite color?" color echo "Your favorite color is $color" -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard