What if your password could stay invisible even when you type it in a script?
Why Silent input with read -s (passwords) in Bash Scripting? - Purpose & Use Cases
Imagine you need to type your password on a shared computer or in front of others. You type it, but everyone can see the characters on the screen. This feels unsafe and uncomfortable.
Typing passwords openly is risky. Others can glance and steal your password. Also, if you copy-paste passwords, they might get saved in command history. Manually hiding input is tricky and often forgotten.
The read -s command in bash lets you type input silently. It hides what you type, so no one can see your password on the screen. This keeps your secrets safe and your mind at ease.
read -p "Enter password: " password # password visible as typed
read -s -p "Enter password: " password # password hidden as typed
You can securely ask for sensitive information in scripts without exposing it on the screen.
A script that asks for your database password silently before connecting, so no one nearby can see it.
Typing passwords openly risks security.
read -s hides input for privacy.
It makes scripts safer and more professional.
Practice
-s option do when used with the read command in bash scripting?Solution
Step 1: Understand the purpose of
Theread -s-soption inreadhides the input typed by the user, making it silent on the terminal.Step 2: Compare with other options
Other options do not hide input;-sis specifically for silent input, useful for passwords.Final Answer:
It hides the user input on the terminal (silent input). -> Option AQuick Check:
read -s= silent input [OK]
- Thinking -s shows input in uppercase
- Confusing -s with saving input to file
- Assuming -s pauses the script
Solution
Step 1: Recall correct option order for
The optionsread-sand-pmust come before the variable name.Step 2: Check each option's position
read -s -p "Enter password:" password correctly places-sand-pbefore the variablepassword.Final Answer:
read -s -p "Enter password:" password -> Option CQuick Check:
Options before variable = correct syntax [OK]
- Placing options after variable name
- Mixing order of -s and -p incorrectly
- Using -p without quotes for prompt
read -s -p "Enter secret: " secret echo "You entered: $secret"
Solution
Step 1: Analyze the
The prompt string is shown, but the input typed is hidden (silent).read -s -pbehaviorStep 2: Understand the echo output
After input, the script prints the entered value with the message.Final Answer:
The prompt 'Enter secret: ' appears, input is hidden, then prints 'You entered: [input]'. -> Option DQuick Check:
read -s -pshows prompt, hides input [OK]
- Assuming input is visible with -s
- Thinking prompt won't show
- Expecting syntax error
read -p "Enter password:" password -s echo "Password is $password"
Solution
Step 1: Check option placement in
Options likeread-smust come before the variable name; here it is after.Step 2: Understand the consequences
Placing-safter the variable name treats it as another variable name instead of an option, so input is not hidden.Final Answer:
The-soption is placed after the variable name. -> Option BQuick Check:
Options after variable = treated as names [OK]
- Placing options after variable name
- Assuming the script hides input correctly
- Assuming prompt needs no quotes
Solution
Step 1: Verify correct syntax for silent input with prompt
Theread -s -p "Enter password: " pass read -s -p "Confirm password: " pass2 if [[ "$pass" == "$pass2" ]]; then echo "Passwords match"; else echo "Mismatch"; fiusesread -s -p "prompt" variablecorrectly for both inputs.Step 2: Check password comparison syntax
Theread -s -p "Enter password: " pass read -s -p "Confirm password: " pass2 if [[ "$pass" == "$pass2" ]]; then echo "Passwords match"; else echo "Mismatch"; fiuses double brackets and quotes around variables, which is safe and correct for string comparison.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 AQuick 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]
read -s -p and double brackets with quotes for comparison [OK]- Placing options after variable name
- Using single brackets without quotes for strings
- Incorrect order of arguments in read
Ready to prove it?
Pass the test and this pattern moves unknown
