Bash User Input and Output
Every useful script either displays information to the user or accepts information from the user. Bash provides straightforward built-in tools for both input and output operations.
Output with echo
The echo command prints text to the terminal screen. It is the most commonly used output command in Bash.
#!/bin/bash echo "Welcome to eStudy247 Bash Course"
Output:
Welcome to eStudy247 Bash Course
echo Options
| Option | Effect | Example |
|---|---|---|
echo "text" | Prints text with a newline at the end | echo "Hello" |
echo -n "text" | Prints text without a newline | echo -n "Enter name: " |
echo -e "text" | Enables special escape characters | echo -e "Line1\nLine2" |
Escape Characters with echo -e
| Escape | Meaning |
|---|---|
\n | New line |
\t | Tab space |
\\ | Backslash |
#!/bin/bash echo -e "Name:\tAlice\nCity:\tDelhi"
Output:
Name: Alice City: Delhi
Output with printf
The printf command gives more precise control over output formatting. It works like printf in C programming.
#!/bin/bash printf "Name: %s\nAge: %d\n" "Alice" 25
Output:
Name: Alice Age: 25
Format Specifiers for printf
| Specifier | Meaning |
|---|---|
%s | String |
%d | Integer number |
%f | Floating point number |
%.2f | Float with 2 decimal places |
Reading User Input with read
The read command waits for the user to type something and press Enter. The typed value gets stored in a variable.
#!/bin/bash echo "Enter your name:" read username echo "Hello, $username!"
Terminal interaction:
Enter your name: Alice Hello, Alice!
read with Prompt (-p flag)
Instead of using a separate echo line, the prompt can be included directly in the read command using -p.
#!/bin/bash read -p "Enter your city: " city echo "You live in $city."
Output:
Enter your city: Mumbai You live in Mumbai.
read with Silent Input (-s flag)
The -s flag hides the input as the user types. This is commonly used for passwords.
#!/bin/bash read -sp "Enter password: " pass echo "" echo "Password accepted."
The echo "" after the read adds a new line because the -s flag suppresses the automatic newline.
Reading Multiple Values in One Line
Multiple variables can be filled from a single read command by separating values with spaces.
#!/bin/bash read -p "Enter first and last name: " fname lname echo "First: $fname" echo "Last: $lname"
Terminal interaction:
Enter first and last name: John Doe First: John Last: Doe
Reading Input with a Timeout (-t flag)
The -t flag sets a time limit in seconds for the user to respond.
#!/bin/bash read -t 5 -p "Press Enter within 5 seconds: " answer if [ $? -ne 0 ]; then echo "Time expired!" fi
Input and Output Flow Diagram
┌────────────────────────────────────────────────┐ │ Script Execution Flow │ │ │ │ Script starts │ │ │ │ │ ▼ │ │ echo "Enter name:" ──► Shows text on screen │ │ │ │ │ ▼ │ │ read name ──► Waits for keyboard │ │ │ │ │ ▼ │ │ echo "Hello, $name" ──► Shows result │ └────────────────────────────────────────────────┘
Reading Input from a File
The read command can also read content line by line from a file using a while loop.
#!/bin/bash while read line do echo "Line: $line" done < fruits.txt
If fruits.txt contains:
Apple Banana Mango
Output:
Line: Apple Line: Banana Line: Mango
Practical Example – Simple User Form
#!/bin/bash echo "===== Student Registration =====" read -p "Enter student name: " sname read -p "Enter student age: " sage read -p "Enter student city: " scity echo "" echo "--- Registration Details ---" printf "Name : %s\n" "$sname" printf "Age : %d\n" "$sage" printf "City : %s\n" "$scity"
Output:
===== Student Registration ===== Enter student name: Ravi Enter student age: 22 Enter student city: Pune --- Registration Details --- Name : Ravi Age : 22 City : Pune
Key Takeaways
- Use
echofor simple output andprintffor formatted output. - Use
readto accept input from the user. - The
-pflag adds a prompt message directly to thereadcommand. - The
-sflag hides typed input — useful for passwords. - The
-tflag adds a timeout to the input. - The
readcommand can also process file contents line by line inside a loop.
