Bash User Input : Mastering user input in Bash is a fundamental skill every shell scripter must acquire. In this guide, we’ll walk through the modern and practical way of reading input from users using the read
command in Bash, tailored for 2025 and beyond. Whether you’re automating system tasks, building CLI tools, or creating interactive scripts, handling user input effectively is key to writing dynamic and responsive scripts.
We’ll cover everything from basic single-line inputs to advanced input handling—like silent password inputs, setting default values, and reading multiple variables at once. By the end of this tutorial, you’ll not only understand how to use read
, but you’ll also be able to create scripts that interact intelligently with users.
Designed with beginners in mind and polished with pro-level tricks, this guide ensures your journey from novice to Bash input master is smooth and efficient.
Bash User Input :
read
Command Basics
The read
command is used to take input from the user.
Syntax:
read variable_name
Example 1: Basic User Input
#!/bin/bash
echo "Enter your name:"
read name
echo "Hello, $name!"
Example 2: Prompt Inline with -p
#!/bin/bash
read -p "Enter your favorite programming language: " language
echo "Nice! You like $language."
Example 3: Silent Input (useful for passwords) with -s
#!/bin/bash
read -sp "Enter your password: " password
echo
echo "Password entered."
The input won’t be shown on screen while typing.
Example 4: Reading Multiple Inputs
#!/bin/bash
read -p "Enter your first and last name: " fname lname
echo "First Name: $fname"
echo "Last Name : $lname"
Example 5: Using Input in Conditions
#!/bin/bash
read -p "Are you sure you want to continue? (yes/no): " answer
if [[ "$answer" == "yes" ]]; then
echo "Continuing..."
else
echo "Exiting..."
fi
Summary of Useful read
Options:
Option | Description |
---|---|
-p | Prompt inline before reading input |
-s | Silent input (doesn’t echo input) |
-t | Timeout for input in seconds |
-n | Limit number of characters to read |
Awesome Nish! Here’s a complete interactive shell script that acts like a simple menu-driven program, taking input from the user and responding accordingly.
Script: menu.sh
#!/bin/bash
echo "Welcome to the Interactive Menu Program!"
echo
while true; do
echo "========== MENU =========="
echo "1. Show current date and time"
echo "2. Show current logged-in user"
echo "3. List files in current directory"
echo "4. Show system uptime"
echo "5. Exit"
echo "=========================="
read -p "Enter your choice [1-5]: " choice
case $choice in
1)
echo "📅 Date and Time: $(date)"
;;
2)
echo "👤 Logged-in User: $USER"
;;
3)
echo "📂 Files in $(pwd):"
ls
;;
4)
echo "⏱️ System Uptime:"
uptime
;;
5)
echo "Goodbye, $USER!"
break
;;
*)
echo "❌ Invalid choice! Please select from 1 to 5."
;;
esac
echo
done
How to Run:
- Save as
menu.sh
- Make it executable:
chmod +x menu.sh
- Run it:
./menu.sh
What it Demonstrates:
- Taking input using
read
- Using
case
for control flow - Calling system commands (
date
,ls
,uptime
) - User interaction in a loop
- Graceful exit
You can also Visit other tutorials of Embedded Prep
- What is eMMC (Embedded MultiMediaCard) memory ?
- Top 30+ I2C Interview Questions
- Bit Manipulation Interview Questions
- Structure and Union in c
- Little Endian vs. Big Endian: A Complete Guide
- Merge sort algorithm
Special thanks to @mr-raj for contributing to this article on Embedded Prep
Leave a Reply