Przejdลบ do treล›ci

๐Ÿ”€ Redirections

Redirection allows you to control where input comes from and where output goes โ€” beyond just printing to the terminal.

๐Ÿงญ Input/Output Streams

Every process has three standard streams:

Stream Number Direction
stdin 0 Input
stdout 1 Output
stderr 2 Errors

By default: - stdin reads from keyboard - stdout prints to terminal - stderr prints to terminal

But you can redirect any of them.


โžก๏ธ Redirecting Output

Send output to a file instead of terminal:

1
$ ls > listing.txt

Append to a file:

1
$ date >> log.txt

Redirect both stdout and stderr:

1
2
3
$ command > output.log 2>&1
# or shorter:
$ command &> output.log

Redirect only stderr:

1
$ command 2> errors.log


โฌ…๏ธ Redirecting Input

Feed data into a command from a file:

1
$ wc < myfile.txt

Pass inline text using here-documents:

1
2
3
$ cat << EOF
Hello World
EOF

Useful in scripts:

1
2
3
4
5
mail recipient@example.com << EOF
Subject: Test Email

Body content here.
EOF


๐Ÿงช Combining Redirections

You can combine multiple redirections in one line:

1
$ grep ERROR logs.txt > errors_found.log 2> grep_errors.log

This separates matched lines (stdout) from potential grep errors (stderr).

Also useful:

1
$ some_command 2>&1 | tee output_and_log.txt

This sends both output and errors through pipe while saving to a file.


๐Ÿงพ Summary

  • Redirection lets you reroute I/O streams.
  • Common operators: >, >>, <, <<, 2>, &>
  • Combine with pipes (|) for powerful workflows.
  • Always consider where data should flow in automated scripts.

๐Ÿ‘‰ Continue to: Pipelines