0
0
Bash Scriptingscripting~20 mins

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

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