Introduction
Sometimes you need to ask for a password or secret without showing it on the screen. Silent input hides what you type.
read -s variable_name
read -s password
read -s -p "Enter your password: " password echo "Password received."
#!/bin/bash read -s -p "Enter your password: " password echo echo "You entered a password with length: ${#password}"
-s option do when used with the read command in bash scripting?read -s-s option in read hides the input typed by the user, making it silent on the terminal.-s is specifically for silent input, useful for passwords.read -s = silent input [OK]read-s and -p must come before the variable name.-s and -p before the variable password.read -s -p "Enter secret: " secret echo "You entered: $secret"
read -s -p behaviorread -s -p shows prompt, hides input [OK]read -p "Enter password:" password -s echo "Password is $password"
read-s must come before the variable name; here it is after.-s after the variable name treats it as another variable name instead of an option, so input is not hidden.-s option is placed after the variable name. -> Option Bread -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.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.read -s -p and double brackets with quotes for comparison [OK]Pass the test and this pattern moves unknown