Guide
Bash-Scripting
Beginner
User Input
Reading interactive input with read, prompts, hidden password input, timeouts, and reading input from a file or pipe.
Basic Input: read
echo "Enter your name:"
read NAME
echo "Hello, $NAME"
read -p "Enter your name: " NAME # prompt on the same line, more common in real scripts
Reading Multiple Values
read -p "Enter first and last name: " FIRST LAST
echo "First: $FIRST, Last: $LAST"
# If more words are typed than variables given, the LAST variable gets everything remaining
read -p "Enter a sentence: " WORD1 REST
Silent Input (Passwords)
read -sp "Enter password: " PASSWORD
echo # newline, since -s suppresses the Enter keypress's own newline
echo "Password length: ${#PASSWORD}"
Input With a Timeout
if read -t 5 -p "Continue? (y/n): " ANSWER; then
echo "You answered: $ANSWER"
else
echo "No response in 5 seconds, defaulting to 'n'"
ANSWER="n"
fi
Reading a Fixed Number of Characters
read -n 1 -p "Press any key to continue..." KEY
echo
Reading From a File, Line by Line
while IFS= read -r LINE; do
echo "Processing: $LINE"
done < servers.txt
IFS=prevents leading/trailing whitespace from being stripped.-rprevents backslashes from being interpreted as escape characters — almost always what you want when reading raw file content.
Reading From a Pipe
echo "piped value" | read RESULT
echo "$RESULT" # EMPTY! read ran in a subshell, its variable didn't survive
# Correct approach: process substitution avoids the subshell problem
read RESULT < <(echo "piped value")
echo "$RESULT" # works correctly
Validating Input
read -p "Enter a number: " NUM
if ! [[ "$NUM" =~ ^[0-9]+$ ]]; then
echo "Error: not a valid number" >&2
exit 1
fi
Non-Interactive Scripts (CI/Automation)
# Scripts meant to run unattended (cron, CI) should NEVER block on read.
# Provide a default or read from an argument/environment variable instead:
ENVIRONMENT="${1:-production}" # use $1 if given, else default to "production"
Production Considerations
- Automated scripts (cron jobs, CI pipelines) have no terminal attached — a stray
readwill hang indefinitely; always guard interactive prompts or provide non-interactive fallbacks. - Always quote
read -rwhen consuming file content — without-r, a trailing backslash silently eats the next line. - Never
reada plaintext password into a variable that then gets logged or passed as a command-line argument (visible inps aux) — use-sand pass secrets via environment variables or files with restricted permissions instead.
Quick Interview Answer
“
readcaptures input into variables —-pshows a prompt,-shides input like passwords,-tadds a timeout, and-rprevents backslash escaping (important when reading files). Piping intoreaddirectly doesn’t work because the pipeline runs in a subshell whose variables don’t survive — process substitution (< <(command)) is the fix.”
Common Mistakes
- Piping into
readand being confused why the variable is empty afterward (subshell scoping). - Leaving an interactive
readprompt in a script meant to run unattended via cron/CI, causing it to hang forever. - Not validating user input before using it in a command, opening the door to unexpected behavior or injection.
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form