Kahibaro
Discord Login Register

2.3.4 Input/output redirection

Understanding Standard Input, Output, and Error

Many command-line tools read from standard input (stdin) and write to standard output (stdout) and standard error (stderr).

Numbered streams:

By default:

Redirection lets you change where these streams come from and go to: files, other commands, or nowhere.

Redirecting Standard Output (`>` and `>>`)

Overwriting a file with `>`

Use > to send stdout to a file, overwriting the file if it exists:

echo "Hello" > greeting.txt
ls > file_list.txt

Effects:

Common patterns:

uname -a > system-info.txt      # Save system info
date > last-run.txt             # Record the last time a command was run

Be careful with >: existing data will be lost.

Appending to a file with `>>`

Use >> to add output to the end of a file, preserving existing contents:

echo "Another line" >> notes.txt
ls *.log >> log-files.txt

Typical uses:

date >> backup-history.txt
echo "Backup completed" >> backup-history.txt

Summary:

Redirecting Standard Input (`<`)

Most commands read from stdin. Instead of typing input manually, you can take it from a file:

wc -l < file.txt

This is roughly equivalent to:

wc -l file.txt

but uses stdin instead of a filename argument.

Another example:

sort < unsorted.txt

You’ll see stdin redirection most with programs that can read from stdin by default, like cat, sort, wc, etc.

Redirecting Standard Error

Some commands write errors to stderr (stream 2). You can redirect it separately from stdout.

Send stderr to a file

command 2> errors.log

Examples:

ls /no/such/dir 2> errors.log

Only error messages go into errors.log. Normal output still appears on the screen.

Append errors instead of overwriting:

command 2>> errors.log

Redirect stdout and stderr separately

You can use two redirections in one command:

command > output.log 2> errors.log

Combining stdout and stderr

Sometimes you want both normal output and errors in the same file.

Redirect stderr to the same place as stdout

Bash-style syntax:

command > all.log 2>&1

Explanation:

Order matters. These two are different:

Another variant (Bash-only):

command &> all.log       # stdout and stderr both to all.log (overwrite)
command &>> all.log      # stdout and stderr both to all.log (append)

Discarding Output with `/dev/null`

/dev/null is a “black hole” for data: anything sent there is thrown away.

Common uses:

  command > /dev/null
  command 2> /dev/null
  command > /dev/null 2>&1
  # or
  command &> /dev/null

This is useful for commands you care about only succeeding, not what they print.

Using Pipes (`|`) to Connect Commands

A pipe sends stdout of one command as stdin to another:

command1 | command2

Examples:

ls | less
dmesg | grep usb
ps aux | grep firefox

Key points:

To include stderr in the pipe (Bash):

command1 2>&1 | command2
# or
command1 |& command2

Example:

make 2>&1 | tee build.log

Here-Documents (`<<`) and Here-Strings (`<<<`)

Sometimes you want to feed multiple lines of text directly into a command, without creating a separate file.

Here-document (`<<`)

A here-document lets you embed a block of text in your command line or script.

General pattern:

command <<EOF
line 1
line 2
line 3
EOF

EOF is a delimiter word you choose; it marks the start and end of the block. Any word can be used as long as it matches at both ends.

Examples:

cat <<END
This is line one.
This is line two.
END

Or for tools that read stdin, like wc:

wc -l <<TEXT
first line
second line
third line
TEXT

Here-string (`<<<`)

A here-string sends a single string as stdin:

tr 'a-z' 'A-Z' <<< "hello world"

Equivalently:

echo "hello world" | tr 'a-z' 'A-Z'

Another example:

wc -w <<< "one two three four"

Here-strings are Bash-specific and not POSIX sh.

Combining Redirection and Pipes

You can mix and match redirections and pipes in one command line.

Examples:

  ls -l | tee files.log

tee reads stdin and writes it to both stdout (screen) and a file.

  some_command >> results.txt 2> /dev/null
  some_command 2> errors.log | grep "success"

Order of redirections and pipes generally follows:

  1. Command and its arguments
  2. Pipe(s)
  3. Redirections

Practical Examples

Save a directory listing and view it later

ls -l > dir-list.txt
less dir-list.txt

Count only error messages from a program

program 1> /dev/null 2> errors.log
wc -l errors.log

Log both output and errors while still seeing them

program 2>&1 | tee program.log

Run a quiet command, only react on exit status

backup.sh > /dev/null 2>&1
echo "Backup completed with status $?"

These patterns are the foundation for automating tasks and chaining powerful command-line workflows.

Views: 73

Comments

Please login to add a comment.

Don't have an account? Register now!