Static scripts are useful, but truly powerful bash scripts respond to user input and adapt their behavior accordingly. Whether you're building configuration wizards, data collection tools, or interactive menus, capturing user input is an essential skill for any bash scripter.
๐ฏ What You'll Learn: In this comprehensive guide, you'll discover:
- How to capture user input with the
read
command - Different ways to prompt users for information
- Input validation techniques and best practices
- Building interactive menus and forms
- Handling different types of user input (text, numbers, choices)
Time to read: ~8 minutes | Difficulty: Beginner to Intermediate
๐ Understanding Interactive Scripts
Interactive scripts make your bash programs dynamic and user-friendly. Instead of hard-coding values or requiring command-line arguments, you can ask users for input during script execution, making your scripts more intuitive and flexible.
Prerequisites
Before we dive in, make sure you have:
- Completed the Bash Variables Guide tutorial
- Understanding of variable assignment and usage
- Familiarity with basic bash script creation
๐ Your First Interactive Script
Let's start with a simple greeting script that asks for the user's name:
#!/bin/bash
echo "What is your name?"
read user_name
echo "Nice to meet you $user_name!"
When you run this script, here's what happens:
$ ./greet_user.sh
What is your name?
Yahya
Nice to meet you Yahya!
Let's break down what each line does:
The Echo and Read Pattern
๐ค Echo Command
echo "What is your name?"
- Displays a prompt to the user
- Creates clear communication
- Sets expectations for input
๐ฅ Read Command
read user_name
- Pauses script execution
- Waits for user to type and press Enter
- Stores input in the
user_name
variable
โ
How It Works: The read
command pauses your script and waits for the user to type something and press Enter. Whatever they type gets stored in the variable you specify.
๐ ๏ธ Read Command Variations
The basic read
command is just the beginning. Let's explore different ways to capture user input:
Inline Prompts
Instead of using separate echo
and read
commands, you can combine them:
#!/bin/bash
# Inline prompt with -p flag
read -p "What is your name? " user_name
read -p "How old are you? " age
read -p "What city are you from? " city
echo ""
echo "=== Your Information ==="
echo "Name: $user_name"
echo "Age: $age"
echo "City: $city"
Output:
$ ./inline-prompt.sh
What is your name? Alice
How old are you? 28
What city are you from? Seattle
=== Your Information ===
Name: Alice
Age: 28
City: Seattle
Silent Input (Passwords)
For sensitive information like passwords, use the -s
flag to hide input:
#!/bin/bash
read -p "Username: " username
read -sp "Password: " password
echo "" # Add a newline since -s doesn't show the Enter
echo "Login attempt for user: $username"
# Note: Never echo passwords in real scripts!
โ ๏ธ Security Note: The -s
flag hides the input from display, but the password is still stored in plain text in the variable. For production scripts, consider using more secure methods.
Input with Timeouts
You can set timeouts to prevent scripts from hanging indefinitely:
#!/bin/bash
echo "Quick! You have 5 seconds to enter your name:"
if read -t 5 -p "Name: " quick_name; then
echo "Hello $quick_name! You're quick!"
else
echo ""
echo "Too slow! Using default name: Guest"
quick_name="Guest"
fi
echo "Welcome, $quick_name!"
Reading Multiple Values
You can read multiple values at once:
#!/bin/bash
echo "Enter your first and last name:"
read first_name last_name
echo "Enter three favorite colors:"
read color1 color2 color3
echo ""
echo "Full Name: $first_name $last_name"
echo "Favorite Colors: $color1, $color2, $color3"
๐ฏ Practical Interactive Scripts
Example 1: Simple Calculator
#!/bin/bash
echo "=== Simple Calculator ==="
read -p "Enter first number: " num1
read -p "Enter second number: " num2
echo ""
echo "Choose operation:"
echo "1) Addition (+)"
echo "2) Subtraction (-)"
echo "3) Multiplication (*)"
echo "4) Division (/)"
read -p "Enter your choice (1-4): " choice
case $choice in
1)
result=$((num1 + num2))
operation="+"
;;
2)
result=$((num1 - num2))
operation="-"
;;
3)
result=$((num1 * num2))
operation="*"
;;
4)
if [ $num2 -ne 0 ]; then
result=$((num1 / num2))
operation="/"
else
echo "Error: Cannot divide by zero!"
exit 1
fi
;;
*)
echo "Invalid choice!"
exit 1
;;
esac
echo ""
echo "Result: $num1 $operation $num2 = $result"
Example 2: File Manager Script
#!/bin/bash
echo "=== Simple File Manager ==="
echo "Current directory: $(pwd)"
echo ""
while true; do
echo "What would you like to do?"
echo "1) List files"
echo "2) Create a file"
echo "3) Delete a file"
echo "4) Exit"
read -p "Enter your choice (1-4): " choice
echo ""
case $choice in
1)
echo "Files in current directory:"
ls -la
;;
2)
read -p "Enter filename to create: " filename
if [ -n "$filename" ]; then
touch "$filename"
echo "File '$filename' created successfully!"
else
echo "Invalid filename!"
fi
;;
3)
read -p "Enter filename to delete: " filename
if [ -f "$filename" ]; then
read -p "Are you sure you want to delete '$filename'? (y/N): " confirm
if [[ $confirm =~ ^[Yy]$ ]]; then
rm "$filename"
echo "File '$filename' deleted!"
else
echo "Deletion cancelled."
fi
else
echo "File '$filename' not found!"
fi
;;
4)
echo "Goodbye!"
exit 0
;;
*)
echo "Invalid choice! Please try again."
;;
esac
echo ""
done
๐ Input Validation Techniques
Always validate user input to make your scripts robust:
Basic Validation
#!/bin/bash
# Validate non-empty input
while true; do
read -p "Enter your name (required): " name
if [ -n "$name" ]; then
break
else
echo "Name cannot be empty! Please try again."
fi
done
# Validate numeric input
while true; do
read -p "Enter your age: " age
if [[ $age =~ ^[0-9]+$ ]] && [ $age -gt 0 ] && [ $age -lt 150 ]; then
break
else
echo "Please enter a valid age (1-149)."
fi
done
# Validate email format (basic)
while true; do
read -p "Enter your email: " email
if [[ $email =~ ^[^@]+@[^@]+\.[^@]+$ ]]; then
break
else
echo "Please enter a valid email address."
fi
done
echo ""
echo "=== Validated Information ==="
echo "Name: $name"
echo "Age: $age"
echo "Email: $email"
Choice Validation
#!/bin/bash
echo "Select your favorite programming language:"
echo "1) Bash"
echo "2) Python"
echo "3) JavaScript"
echo "4) Go"
while true; do
read -p "Enter your choice (1-4): " choice
case $choice in
1)
language="Bash"
break
;;
2)
language="Python"
break
;;
3)
language="JavaScript"
break
;;
4)
language="Go"
break
;;
*)
echo "Invalid choice! Please enter 1, 2, 3, or 4."
;;
esac
done
echo "Great choice! $language is awesome!"
๐จ Building Interactive Menus
Create professional-looking menus for better user experience:
#!/bin/bash
show_menu() {
clear
echo "================================="
echo " System Administration Menu "
echo "================================="
echo "1) System Information"
echo "2) Disk Usage"
echo "3) Memory Usage"
echo "4) Running Processes"
echo "5) Network Information"
echo "6) Exit"
echo "================================="
}
while true; do
show_menu
read -p "Please select an option (1-6): " choice
echo ""
case $choice in
1)
echo "=== System Information ==="
uname -a
read -p "Press Enter to continue..."
;;
2)
echo "=== Disk Usage ==="
df -h
read -p "Press Enter to continue..."
;;
3)
echo "=== Memory Usage ==="
free -h
read -p "Press Enter to continue..."
;;
4)
echo "=== Top 10 Processes ==="
ps aux | head -11
read -p "Press Enter to continue..."
;;
5)
echo "=== Network Interfaces ==="
ip addr show
read -p "Press Enter to continue..."
;;
6)
echo "Thank you for using the system menu!"
exit 0
;;
*)
echo "Invalid option! Please try again."
read -p "Press Enter to continue..."
;;
esac
done
๐จ Common Pitfalls and Solutions
Empty Input Handling
Problem: Script breaks when user presses Enter without typing anything Solution: Always check if input is empty before using it
read -p "Enter filename: " filename
if [ -z "$filename" ]; then
echo "Filename cannot be empty!"
exit 1
fi
Special Characters in Input
Problem: User input with spaces or special characters breaks the script Solution: Always quote variables when using them
read -p "Enter path: " user_path
# Wrong
cd $user_path
# Correct
cd "$user_path"
Infinite Loops
Problem: Validation loops never exit when user keeps entering invalid input Solution: Add a maximum retry limit
attempts=0
max_attempts=3
while [ $attempts -lt $max_attempts ]; do
read -p "Enter valid input: " input
if validate_input "$input"; then
break
fi
attempts=$((attempts + 1))
echo "Invalid input. Attempt $attempts of $max_attempts"
done
if [ $attempts -eq $max_attempts ]; then
echo "Too many invalid attempts. Exiting."
exit 1
fi
๐งช Practice Exercise
Create a user registration script that demonstrates multiple input techniques:
#!/bin/bash
echo "=== User Registration System ==="
echo ""
# Full name with validation
while true; do
read -p "Full Name: " full_name
if [ -n "$full_name" ]; then
break
else
echo "Name is required!"
fi
done
# Email with basic validation
while true; do
read -p "Email: " email
if [[ $email =~ ^[^@]+@[^@]+\.[^@]+$ ]]; then
break
else
echo "Please enter a valid email!"
fi
done
# Age with numeric validation
while true; do
read -p "Age: " age
if [[ $age =~ ^[0-9]+$ ]] && [ $age -ge 18 ]; then
break
else
echo "Please enter a valid age (18 or older)!"
fi
done
# Password (hidden input)
read -sp "Password: " password
echo ""
# Confirm password
read -sp "Confirm Password: " confirm_password
echo ""
if [ "$password" = "$confirm_password" ]; then
echo ""
echo "=== Registration Successful ==="
echo "Name: $full_name"
echo "Email: $email"
echo "Age: $age"
echo "Account created successfully!"
else
echo "Passwords don't match! Registration failed."
exit 1
fi
๐ฏ Key Takeaways
โ Remember These Points
- Always Validate Input: Never trust user input - validate everything
- Quote Variables: Always use
"$variable"
to handle spaces and special characters - Provide Clear Prompts: Make it obvious what input you expect
- Handle Empty Input: Check for empty strings before processing
- Use Timeouts When Appropriate: Prevent scripts from hanging indefinitely
๐ What's Next?
Now that you can capture user input, you can create truly interactive and user-friendly scripts:
๐ Further Reading
Official Resources
๐ Outstanding Progress! You now know how to create interactive bash scripts that respond to user input. This opens up countless possibilities for building user-friendly automation tools.
What interactive script are you planning to build? Share your ideas in the comments!
๐ฌ Discussion
I'd love to hear about your interactive scripting projects:
- What's the most useful interactive script you've created or want to create?
- Have you encountered any tricky input validation scenarios?
- What types of user interfaces work best for command-line tools?
- Any specific input handling techniques you'd like me to cover?
Connect with me:
- ๐ GitHub - Interactive script examples and templates
- ๐ง Contact - Scripting questions and project discussions