Using Shell Redirection and Piping
The Unix/Linux philosophy revolves around the concept of programs as building blocks—each one intended to do one job and do it well. Redirection lets you connect these commands to files, and piping enables you to plug commands together like a child’s toy.
How Do I Do That?
Each command has three numbered file descriptors that are opened automatically:
- standard input (stdin, file descriptor 0)
The normal input to the program
- standard output (stdout, file descriptor 1)
The normal output from the program
- standard error (stderr, file descriptor 2)
Error messages from the program
By default, these file descriptors are connected to the terminal, if one is available, so standard input comes from the terminal keyboard, and standard output and standard error go to the terminal screen. Programs may open any other connections they need to read or write files, communicate with other local programs, or communicate with programs over the network.
Tip
Connections to the graphical user interface are created by opening a network connection from the program (client) to the X Window server. This is distinct from the three standard file descriptors.
Redirection
To redirect the output of a program to a file, use the
greater-than (>) symbol followed by the name of the file:
$cal 7 2006July 2006 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 $cal 7 2006 >$month.txtcatmonth.txtJuly 2006 Su Mo Tu We Th ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access