Input — Accepting User Input in Bash Scripts¶
Input allows Bash scripts to interact with users, accept command-line arguments, read data from files, and process information from standard input. Interactive scripts become more flexible because they can work with different values each time they are executed instead of relying on hardcoded data. Every Linux administrator, DevOps engineer, Cloud Architect, Platform Engineer, and Site Reliability Engineer (SRE) should understand how to safely collect and validate user input in production scripts.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Read user input
- Use command-line arguments
- Display interactive prompts
- Validate user input
- Read passwords securely
- Use default values
- Process multiple inputs
- Apply input handling in production scripts
Prerequisites¶
Complete:
- Modules 1–9
- Module 10 Lessons 1–5
Why Learn Input?¶
Imagine creating a user account script.
Without input:
The script creates only one user.
Using input:
The same script can create any user.
What is Input?¶
Input is data provided to a script.
It may come from:
- User keyboard input
- Command-line arguments
- Files
- Pipes
- Other commands
Reading User Input¶
Basic syntax:
Example:
Reading with a Prompt¶
Instead of using echo, display the prompt directly.
Reading Multiple Values¶
Input:
Output:
Reading Passwords¶
Hide user input while typing.
The password is not displayed on the screen.
Command-Line Arguments¶
Arguments are values passed when running a script.
Example:
Inside the script:
Output:
Special Variables¶
| Variable | Description |
|---|---|
$0 | Script name |
$1 | First argument |
$2 | Second argument |
$@ | All arguments |
$# | Number of arguments |
$$ | Current process ID |
$? | Exit status of previous command |
Checking Argument Count¶
Loop Through Arguments¶
Using Default Values¶
If no argument is supplied:
Input Validation¶
Example:
Confirm User Action¶
read -p "Continue? (y/n): " ANSWER
if [[ "$ANSWER" == "y" ]]
then
echo "Continuing..."
else
echo "Cancelled."
fi
Reading a File¶
Reading from a Pipe¶
Common Commands¶
Read input.
Prompt user.
Read password.
Display arguments.
Argument count.
Real Production Examples¶
Create user.
Accept deployment environment.
Restart a service.
Production Perspective¶
Input handling is commonly used in:
- Deployment scripts
- Backup automation
- User management
- Cloud provisioning
- Infrastructure automation
- Monitoring tools
- Configuration scripts
- Interactive administration utilities
Proper validation helps prevent errors and improves script reliability.
Hands-on Lab¶
Task 1¶
Read a user's name.
Task 2¶
Read multiple values.
Task 3¶
Read a password.
Task 4¶
Display the first argument.
Run:
Task 5¶
Display all arguments.
Task 6¶
Display the argument count.
Task 7¶
Validate numeric input.
Task 8¶
Read a file line by line.
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
read | Read user input | Interactive scripts |
read -p | Display prompt | User-friendly input |
read -s | Read password | Secure authentication |
$1, $2 | Command-line arguments | Deployment scripts |
$@ | All arguments | Batch processing |
$# | Number of arguments | Input validation |
Common Input Mistakes¶
| Mistake | Solution |
|---|---|
| Not validating input | Always validate |
| Assuming arguments exist | Check $# |
| Displaying passwords | Use read -s |
| Forgetting quotes | Quote variables |
| Hardcoding values | Accept user input |
Production Troubleshooting Scenario¶
Scenario
A deployment script fails because no environment name is supplied.
Before:
If no argument is passed:
Improved:
The script now validates input before execution.
Best Practices¶
- Validate all user input.
- Check command-line arguments before use.
- Use secure password input with
read -s. - Quote variables to prevent word splitting.
- Provide meaningful prompts.
- Display usage information for missing arguments.
- Handle invalid input gracefully.
Common Mistakes¶
❌ Assuming users always provide valid input.
✅ Verify users always provide valid input instead of assuming it.
❌ Not checking command-line arguments.
✅ Always checking command-line arguments.
❌ Displaying passwords on the terminal.
✅ Avoid this mistake: displaying passwords on the terminal.
❌ Forgetting to quote input variables.
✅ Remember to to quote input variables.
❌ Proceeding without validating required values.
✅ Avoid this mistake: proceeding without validating required values.
Interview Questions¶
Beginner¶
- What does the
readcommand do? - What is
$1? - What does
$#represent? - How do you securely read a password?
Intermediate¶
- What is the difference between keyboard input and command-line arguments?
- How do you validate user input?
- What does
$@represent? - How do you provide default values for missing arguments?
Architect Level¶
- How would you design secure interactive Bash scripts?
- Why is input validation important in production automation?
- How can improper input handling create security risks?
Summary¶
In this lesson, you learned:
- Reading user input
- Interactive prompts
- Command-line arguments
- Password input
- Input validation
- Default values
- Reading from files
- Production scripting best practices
Input handling enables Bash scripts to interact with users and accept dynamic data. By validating input and handling errors gracefully, you can build secure, reliable, and production-ready automation scripts.
Key Takeaways¶
- Use
readto accept keyboard input. - Use command-line arguments for script flexibility.
- Validate all user-provided data.
- Use
read -sfor passwords. - Check
$#before accessing arguments. - Provide meaningful prompts and usage messages.
What's Next?¶
Exit Codes — Understanding Command Success and Failure in Bash
You'll explore:
- What exit codes are
- Standard Linux exit codes
- Using the
exitcommand - Checking command status
- Using
$? - Returning exit codes from functions
- Production error handling
By the end of the lesson, you'll be able to use exit codes effectively to build reliable Bash scripts that detect failures, communicate status, and integrate seamlessly with automation tools and CI/CD pipelines.