Command Input/Output Redirection

This section provides a tutorial example on how to use command input and output redirection operators to read input files and save output files.

If you want to save the output of command into a new file, or take input from an existing file, you need to understand how Bash handles input and output when executing a command.

When Bash starts a command, it will establish 3 standard communication channels:

To help you changing communication channels, Bash provides the following redirection operators:

Here are some examples of using Bash redirection operators:

# redirect stdout to a file 
herong$ grep earlyoom /var/log/syslog > earlyoom.log

# redirect stdin to a file 
herong$ grep 'Out of' < earlyoom.log
ubuntu earlyoom[10287]: Out of memory! avail: 22 MiB < min: 362 MiB
ubuntu earlyoom[10287]: Out of memory! avail: 24 MiB < min: 362 MiB

# redirect stdout and stderr to different files 
herong$ ping nowhere.com > output.txt 2> error.txt
herong$ cat error.txt 
ping: nowhere.com: Name or service not known

# redirect stdout and stderr to the same file 
herong$ ping nowhere.com &> output.txt
herong$ cat output.txt 
ping: nowhere.com: Name or service not known

# pipe output as input to the next command 
herong$ ps -u | grep root | wc -l 
185

Table of Contents

 About This Book

 Introduction to Ubuntu Systems

 GNOME - Desktop Interface and Environment

Shell - The Command-Line Interpreter

 What Is Shell

 What Is Bash (Bourne Again SHell)

 Create and Run Bash Script

 Bash Command Line Interpretation Steps

 Bash Shell Session Customization

Command Input/Output Redirection

 Shell Session Command History

 "tmux" - Terminal Multiplexer

 Process Management

 Memory Management

 Files and Directories

 APT (Advanced Package Tool)

 Network Connection on Ubuntu

 Internet Networking Tools

 SSH Protocol and ssh/scp Commands

 Administrative Tasks

 References

 Full Version in PDF/EPUB