Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Silent input with read -s (passwords) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Silent Input Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this silent input script?
Consider this Bash script snippet that asks for a password silently and then prints a confirmation message.

What will be the output if the user types "mypassword" when prompted?
Bash Scripting
read -s -p "Enter password: " pass

echo "Password length is ${#pass}"
A
Enter password: 
Password length is 10
B
Enter password: mypassword
Password length is 10
CPassword length is 10
D
Enter password: 
Password length is 0
Attempts:
2 left
💡 Hint
The -s option hides the input, so the typed password won't appear on screen.
📝 Syntax
intermediate
1:00remaining
Which option correctly reads a password silently and stores it in variable 'pw'?
Select the correct Bash command to read a password silently into variable 'pw' with a prompt "Password:".
Aread -s -p "Password:" pw
Bread -p -s "Password:" pw
Cread pw -s -p "Password:"
Dread -sp "Password:"
Attempts:
2 left
💡 Hint
The order of options matters; -s and -p come before the variable name.
🔧 Debug
advanced
1:30remaining
Why does this script show the password when typed?
This script is intended to read a password silently but shows the typed password on screen.

Identify the reason for this behavior.

Script:
read -p "Enter password: " -s password echo "Password entered."
AThe echo command prints the password, revealing it.
BThe terminal does not support silent input.
CThe variable name 'password' is reserved and causes echoing.
DThe -s option must come before -p; order matters.
Attempts:
2 left
💡 Hint
Check the order of options in the read command.
🚀 Application
advanced
2:00remaining
How to confirm password input matches silently?
You want to ask the user to enter a password twice silently and check if both entries match.

Which script snippet correctly implements this?
A
read -s -p "Enter password: " pw1
read -s -p "Confirm password: " pw2
if [[ $pw1 = $pw2 ]]; then echo "Match"; else echo "No match"; fi
B
read -s -p "Enter password: " pw1

echo
read -s -p "Confirm password: " pw2

echo
if [[ "$pw1" == "$pw2" ]]; then echo "Match"; else echo "No match"; fi
C
read -s -p "Enter password: " pw1
read -s -p "Confirm password: " pw2
if [ "$pw1" == "$pw2" ]; then echo "Match"; else echo "No match"; fi
D
read -p "Enter password: " -s pw1
read -p "Confirm password: " -s pw2
if [ $pw1 = $pw2 ]; then echo "Match"; else echo "No match"; fi
Attempts:
2 left
💡 Hint
Use double brackets [[ ]] and quote variables to avoid errors.
🧠 Conceptual
expert
1:30remaining
What happens if you use 'read -s' without '-p' in a script?
In a Bash script, you use 'read -s password' without the -p option.

What will the user experience and what must you do to make the input clear?
AThe input is visible because -p is required to hide input.
BThe terminal shows a default prompt automatically; no extra prompt needed.
CThe user sees no prompt and types blindly; you should print a prompt before the read command.
DThe script will error out because -p is mandatory with -s.
Attempts:
2 left
💡 Hint
Think about user experience when no prompt is shown.

Practice

(1/5)
1. What does the -s option do when used with the read command in bash scripting?
easy
A. It hides the user input on the terminal (silent input).
B. It shows the input characters in uppercase.
C. It saves the input to a file automatically.
D. It pauses the script until a key is pressed.

Solution

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

    The -s option in read hides the input typed by the user, making it silent on the terminal.
  2. Step 2: Compare with other options

    Other options do not hide input; -s is specifically for silent input, useful for passwords.
  3. Final Answer:

    It hides the user input on the terminal (silent input). -> Option A
  4. Quick Check:

    read -s = silent input [OK]
Hint: Remember: -s means silent input, no characters shown [OK]
Common Mistakes:
  • Thinking -s shows input in uppercase
  • Confusing -s with saving input to file
  • Assuming -s pauses the script
2. Which of the following is the correct syntax to silently read a password with a prompt in bash?
easy
A. read -p "Enter password:" password -s
B. read password -p -s "Enter password:"
C. read -s -p "Enter password:" password
D. read password -s -p "Enter password:"

Solution

  1. Step 1: Recall correct option order for read

    The options -s and -p must come before the variable name.
  2. Step 2: Check each option's position

    read -s -p "Enter password:" password correctly places -s and -p before the variable password.
  3. Final Answer:

    read -s -p "Enter password:" password -> Option C
  4. Quick Check:

    Options before variable = correct syntax [OK]
Hint: Options (-s, -p) always come before variable name [OK]
Common Mistakes:
  • Placing options after variable name
  • Mixing order of -s and -p incorrectly
  • Using -p without quotes for prompt
3. What will be the output of this script snippet?
read -s -p "Enter secret: " secret

echo "You entered: $secret"
medium
A. The prompt appears, input is visible, then prints 'You entered: [input]'.
B. The prompt does not appear, input is visible, then prints 'You entered: [input]'.
C. The script throws a syntax error.
D. The prompt 'Enter secret: ' appears, input is hidden, then prints 'You entered: [input]'.

Solution

  1. Step 1: Analyze the read -s -p behavior

    The prompt string is shown, but the input typed is hidden (silent).
  2. Step 2: Understand the echo output

    After input, the script prints the entered value with the message.
  3. Final Answer:

    The prompt 'Enter secret: ' appears, input is hidden, then prints 'You entered: [input]'. -> Option D
  4. Quick Check:

    read -s -p shows prompt, hides input [OK]
Hint: Prompt shows, input hidden, echo prints input [OK]
Common Mistakes:
  • Assuming input is visible with -s
  • Thinking prompt won't show
  • Expecting syntax error
4. Identify the error in this script snippet:
read -p "Enter password:" password -s
echo "Password is $password"
medium
A. The prompt string is missing quotes.
B. The -s option is placed after the variable name.
C. The variable name should be before options.
D. There is no error; the script works fine.

Solution

  1. Step 1: Check option placement in read

    Options like -s must come before the variable name; here it is after.
  2. Step 2: Understand the consequences

    Placing -s after the variable name treats it as another variable name instead of an option, so input is not hidden.
  3. Final Answer:

    The -s option is placed after the variable name. -> Option B
  4. Quick Check:

    Options after variable = treated as names [OK]
Hint: Put all options before variable name to avoid errors [OK]
Common Mistakes:
  • Placing options after variable name
  • Assuming the script hides input correctly
  • Assuming prompt needs no quotes
5. You want to create a bash script that asks the user for a password silently, then confirms the password by asking again silently. Which script snippet correctly implements this?
hard
A. read -s -p "Enter password: " pass read -s -p "Confirm password: " pass2 if [[ "$pass" == "$pass2" ]]; then echo "Passwords match"; else echo "Mismatch"; fi
B. read -p "Enter password: " -s pass read -p "Confirm password: " -s pass2 if [ $pass = $pass2 ]; then echo "Passwords match"; else echo "Mismatch"; fi
C. read -s pass "Enter password: " read -s pass2 "Confirm password: " if [[ $pass == $pass2 ]]; then echo "Passwords match"; else echo "Mismatch"; fi
D. read pass -s -p "Enter password: " read pass2 -s -p "Confirm password: " if [[ "$pass" = "$pass2" ]]; then echo "Passwords match"; else echo "Mismatch"; fi

Solution

  1. Step 1: Verify correct syntax for silent input with prompt

    The read -s -p "Enter password: " pass read -s -p "Confirm password: " pass2 if [[ "$pass" == "$pass2" ]]; then echo "Passwords match"; else echo "Mismatch"; fi uses read -s -p "prompt" variable correctly for both inputs.
  2. Step 2: Check password comparison syntax

    The read -s -p "Enter password: " pass read -s -p "Confirm password: " pass2 if [[ "$pass" == "$pass2" ]]; then echo "Passwords match"; else echo "Mismatch"; fi uses double brackets and quotes around variables, which is safe and correct for string comparison.
  3. Final Answer:

    read -s -p "Enter password: " pass read -s -p "Confirm password: " pass2 if [[ "$pass" == "$pass2" ]]; then echo "Passwords match"; else echo "Mismatch"; fi -> Option A
  4. Quick Check:

    Correct option order and safe comparison = read -s -p "Enter password: " pass read -s -p "Confirm password: " pass2 if [[ "$pass" == "$pass2" ]]; then echo "Passwords match"; else echo "Mismatch"; fi [OK]
Hint: Use read -s -p and double brackets with quotes for comparison [OK]
Common Mistakes:
  • Placing options after variable name
  • Using single brackets without quotes for strings
  • Incorrect order of arguments in read
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