Page Content

Tutorials

Input and Output Handling in Linux Explained with Examples

In this blog we discussed two topics: Command-line syntax and features in linux, Input and Output Handling in Linux.

Command-line syntax and features in linux

The “grammar” of the command line is more important for the shell than simply learning names like ls or cd. To know how the shell decodes quotations, special characters, and patterns will help you advance from executing basic commands to creating robust, automated processes.

The Command Structure

Every command you give the operating system follows to the three-part grammar standard:

command -options arguments

  • Command: The executable program you want to run (e.g., cp for copy).
  • Options (Flags): Prefixed with one or two dashes (- or --), these modify the command’s behavior. For example, ls -h makes file sizes “human-readable.”  
  • Arguments: The data the command acts upon, such as filenames, directory paths, or strings of text.

Also Read About How To Use Shell Command In Linux & Basic Shell Commands

Quoting: Protecting Your Data

The shell interprets spaces as separators between arguments. Quoting is how you tell the shell to treat a group of characters as a single unit.  

  • Single Quotes (' '): The “strongest” quotes. Everything inside is taken literally. The shell will not interpret any special characters or variables.
  • Double Quotes (" "): Use these when you want the shell to ignore spaces but still process variables (starting with $) or backticks.
  • Backticks (` `): An older method used for Command Substitution.

Escape Characters

If you need to use a special character (like a space, a dollar sign, or a quote) as literal text without quoting the whole string, you use the Backslash (\). This is known as “escaping” the character.

  • To reference a file named My Resume.pdf, you would type: cat My\ Resume.pdf
  • To print a dollar sign: echo "The price is \$5.00"

Command Substitution

This feature allows you to take the output of one command and use it as an argument for another. Modern shells use the $(command) syntax for this.  

Example: If you want to create a folder named after today’s date, you can run: mkdir $(date +%Y-%m-%d) The shell runs date first, gets the result (e.g., 2026-01-02), and then passes that result to mkdir.

Wildcards and Globbing

“Globbing” is the shell’s way of using patterns to match multiple filenames at once. This saves you from typing out every single file name.

  • * (The Asterisk): Matches any number of characters.
    • ls *.jpg lists every JPEG file in the folder.
  • ? (The Question Mark): Matches exactly one character.
    • ls file?.txt matches file1.txt or fileA.txt, but not file10.txt.
  • [ ] (Square Brackets): Matches any character inside the brackets.
    • rm image[1-3].png would delete image1.png, image2.png, and image3.png.

Also Read About Explain Different Types Of Linux Shells In Operating System

Summary Table

FeatureSyntaxPrimary Use
Strong Quote' 'Treat everything as literal text.
Weak Quote" "Allow variable expansion while ignoring spaces.
Escape\Make the very next character literal.
Substitution$( )Use a command’s output as an argument.
Wildcard*Match all files that fit a pattern.

Input and Output Handling in Linux

Each and every thing is regarded as a stream of data in the shell’s reality. You can connect small, basic tools together to conduct complicated data processing if you understand how these streams run. To call this idea I/O Redirection.

Input and Output Handling in Linux
Input and Output Handling in Linux

Three Typical Streams

The shell opens three data “pipes” for each command you execute. Controlling where your information goes requires an understanding of them.

  • The data delivered to the program is known as Standard Input (stdin / File Descriptor 0). It originates from your keyboard by default.
  • Standard Output (File Descriptor 1/stdout): This is the data that the software successfully sent. It shows up on your screen by default.
  • A distinct stream reserved only for error messages is called Standard Error (stderr / File Descriptor 2). Error notices will still show up on your screen so you can see what went wrong, even if you reroute your data.

Output Redirection

Sometimes you don’t want the result of a command on your screen; you want it in a file.

  • Overwrite (>): Sends the output to a file, deleting any existing content in that file.
    • Example: ls > files.txt (Saves the list of files to files.txt).
  • Append (>>): Adds the output to the end of a file without deleting what is already there.
    • Example: date >> log.txt (Adds the current timestamp to your log).
  • Redirecting Errors (2>): To capture error messages specifically, use the file descriptor for stderr.
    • Example: ls non_existent_folder 2> error.log.

Also Read About What Is Btrfs File System In Linux? And Btrfs Vs Ext4 Vs XFS

Input Redirection

You can tell a command to pull data from a file instead of waiting for you to type it.

  • Input (<): Sends the contents of a file into a command.
    • Example: sort < names.txt (Reads names from the file and sorts them).

Pipes and Filters

The Pipe (|) is the most powerful operator in the shell. It takes the stdout of the command on the left and plugs it directly into the stdin of the command on the right.

This allows you to create “pipelines” to filter and transform data on the fly.

  • Example 1:cat names.txt | sort | uniq
    • This reads a file, sorts the names alphabetically, and then removes any duplicate entries.
  • Example 2:ls /usr/bin | grep "python" | wc -l
    • This lists all programs, filters for those containing “python,” and then counts how many lines (files) were found.

Summary Table of Operators

OperatorNameFunction
>Redirection (Overwrite)Saves output to a file, replacing content.
>>Redirection (Append)Saves output to the end of a file.
<Input RedirectionFeeds a file’s contents into a command.
2>Error RedirectionSpecifically saves error messages to a file.
``The Pipe

Also Read About XFS File System Commands And Ext4 Vs XFS File System

Hemavathi
Hemavathihttps://govindhtech.com/
Myself Hemavathi graduated in 2018, working as Content writer at Govindtech Solutions. Passionate at Tech News & latest technologies. Desire to improve skills in Tech writing.
Index