USD ($)
$
United States Dollar
Euro Member Countries
India Rupee

Input/Output and User Interaction

Lesson 37/49 | Study Time: 20 Min

Input/output (I/O) operations are core to interacting with users and handling data in Linux shell scripting. Effective I/O handling allows scripts to receive input dynamically, display outputs clearly, and interact meaningfully with users or other software components.

Shell scripting provides simple yet flexible commands for reading input, printing output, and redirecting data streams, enabling automation and interactive usage scenarios.

Gathering User Input

The following are common methods for collecting input from users in shell scripts. They allow scripts to interact dynamically with users.


1. The read Command: The most common method to capture user input from the terminal.

Basic syntax:

bash
read variable_name

It pauses the script and waits for input, storing it in the specified variable.

Example:

bash
echo "Enter your name:"
read name
echo "Hello, $name!"


2. Using read with Prompt: The -p option allows displaying prompt text inline.

bash
read -p "Enter your age: " age
echo "You are $age years old."


3. Reading Multiple Variables: Accepts multiple space-separated inputs.

bash
read first last
echo "First Name: $first, Last Name: $last"


4. Silent Input (-s): Useful for passwords or sensitive information.

bash
read -sp "Enter password: " password
echo
echo "Password entered!"


5. Timeout for Input (-t): Reads input with a timeout in seconds.

bash
read -t 10 -p "Enter choice within 10 seconds: " choice
echo "You selected: $choice"

Displaying Output

Below are the standard tools for displaying script output. They allow clear and readable communication with users.


1. The echo Command: The simplest way to output text or variable values to the terminal.

bash
echo "Hello, World!"
echo "User input was: $name"


2. Options for echo-e enables interpretation of backslash escapes:

bash
echo -e "Line1\nLine2"


3. The printf Command: Provides better control over formatting similar to C's printf.

Syntax example:

bash
printf "Name: %s\nAge: %d\n" "$name" "$age"

Does not add a newline automatically, so it must be included (\n).

Redirecting Input and Output

The following are tools used to connect and redirect command data. They allow chaining and filtering of outputs.


1. Standard Streams:


1. STDIN (standard input) - File descriptor 0

2. STDOUT (standard output) - File descriptor 1

3. STDERR (standard error) - File descriptor 2


2. Output Redirection (> and >>):


  • > overwrites or creates a file:
bash
echo "Hello" > file.txt


  • >> appends to a file:
bash
echo "World" >> file.txt


3. Input Redirection (<): Reads input from a file instead of terminal:

bash
sort < unsorted.txt


4. Pipes (|): Connect the output of one command to the input of another:

bash
ls -l | grep ".txt"


5. Redirecting STDERR:


  • Redirect errors to a file:
bash
command 2> error.log


  • Redirect STDERR and STDOUT to the same file:
bash
command > output.log 2>&1
Samuel Wilson

Samuel Wilson

Product Designer
Profile

Class Sessions

1- What is Linux and Operating System Concepts 2- Linux History and Evolution 3- Linux Distributions and Their Purposes 4- Open Source Software and Licensing 5- Graphical User Interface (GUI) and Desktop Environments 6- Terminal Access and Command-Line Fundamentals 7- Getting Help and Command Documentation 8- File System Hierarchy and Directory Structure 9- Navigating Directories and Listing Contents 10- Creating, Copying, and Moving Files and Directories 11- Deleting Files and Directories 12- Symbolic and Hard Links 13- Understanding File Permissions Model 14- Modifying Permissions and Ownership 15- User and Group Management 16- Sudo and Privilege Escalation 17- Text Searching and Pattern Matching 18- Text Processing and Stream Editing 19- Compressing and Archiving Files 20- Text Editing and File Creation 21- Package Management Systems Overview 22- Installing and Updating Software with APT 23- Installing and Updating Software with YUM/DNF 24- Managing Software from Non-Repository Sources 25- Understanding Processes and Process Management 26- Viewing Running Processes 27- Process Control and Termination 28- Task Scheduling with Cron 29- Networking Concepts and IP Addressing 30- Viewing and Configuring Network Interfaces 31- Basic Network Troubleshooting 32- Shell Script Basics 33- Variables and Data Types 34- Conditional Logic in Scripts 35- Loops and Iteration 36- Functions and Code Reuse 37- Input/Output and User Interaction 38- System Authentication and Access Control 39- File System Security 40- Software Updates and Patching 41- Basic Firewall Concepts 42- System Information and Monitoring 43- Service and Daemon Management 44- System Boot Process and Runlevels 45- System Backup and Disaster Recovery 46- Comprehensive File System Management 47- System Automation Workflows 48- Multi-Concept Troubleshooting Scenarios 49- Continued Learning Pathways

Sales Campaign

Sales Campaign

We have a sales campaign on our promoted courses and products. You can purchase 1 products at a discounted price up to 15% discount.