|
1 | 1 | # About Redirections
|
2 | 2 |
|
3 |
| -TODO |
| 3 | +In the world of the command-line interface (CLI), programs often interact with three standard data streams: |
| 4 | + |
| 5 | +1. **Standard Input** (`stdin`): Where a program receives its input (typically from the keyboard). |
| 6 | +1. **Standard Output** (`stdout`): Where a program sends its normal output (typically to the terminal screen). |
| 7 | +1. **Standard Error** (`stderr`): Where a program sends error messages (typically to the terminal screen). |
| 8 | + |
| 9 | +Recall that we introduced these in the [Pipelines and Command Lists][pipelines] chapter. |
| 10 | + |
| 11 | +Shell redirections are powerful mechanisms that allow you to change the default destinations of these streams. Instead of a program reading from the keyboard or writing to the screen, you can redirect input from a file or send output to a file. This provides flexibility and control over how programs interact with data. |
| 12 | + |
| 13 | +## Key Redirection Operators |
| 14 | + |
| 15 | +Here are the most common redirection operators and what they do: |
| 16 | + |
| 17 | +### `>` (Output Redirection) |
| 18 | + |
| 19 | +* Purpose: Redirects the standard output (stdout) of a command to a file. |
| 20 | +* Behavior: |
| 21 | + * If the file doesn't exist, it's created. |
| 22 | + * If the file exists, its contents are overwritten. |
| 23 | +* Example: |
| 24 | + ```bash |
| 25 | + # Sends the output of 'ls -l' to 'file_listing.txt' |
| 26 | + ls -l > file_listing.txt |
| 27 | + ``` |
| 28 | + |
| 29 | +~~~~exercism/caution |
| 30 | +Redirections are performed **before** the command is executed. |
| 31 | +This means you cannot redirect to the same file you need to read from. |
| 32 | +
|
| 33 | +```bash |
| 34 | +date > date.txt # populate the file with the current date |
| 35 | +cat date.txt > date.txt # 'date.txt' is now empty! |
| 36 | +``` |
| 37 | +
|
| 38 | +In the second command of the above example, the shell |
| 39 | +1. first empties the file named in the redirection, then |
| 40 | +2. reads zero bytes from the empty file, and writes them to the file. |
| 41 | +~~~~ |
| 42 | + |
| 43 | +### `>>` (Append Output Redirection) |
| 44 | + |
| 45 | +* Purpose: Appends the standard output (stdout) of a command to a file. |
| 46 | +* Behavior: |
| 47 | + * If the file doesn't exist, it's created. |
| 48 | + * If the file exists, the output is added to the end of the file. |
| 49 | +* Example: |
| 50 | + ```bash |
| 51 | + # Adds "Another line" to the end of the file |
| 52 | + echo "Another line" >> file_listing.txt |
| 53 | + ``` |
| 54 | + |
| 55 | +### `<` (Input Redirection) |
| 56 | + |
| 57 | +* Purpose: Redirects the standard input (stdin) of a command from a file. |
| 58 | +* Behavior: The command reads its input from the specified file instead of the keyboard. |
| 59 | +* Example: |
| 60 | + ```bash |
| 61 | + # 'sort' reads _from_ 'unsorted_data.txt' and |
| 62 | + # output is redirected _to_ 'sorted_data.txt' |
| 63 | + sort < unsorted_data.txt > sorted_data.txt |
| 64 | + ``` |
| 65 | + |
| 66 | +### `2>` (Error Redirection): |
| 67 | + |
| 68 | +* Purpose: Redirects the standard error (stderr) of a command to a file. |
| 69 | +* Behavior: Error messages are sent to the specified file instead of the terminal. |
| 70 | +* Example: |
| 71 | + ```bash |
| 72 | + rm non_existent_file 2> error_log.txt # Error message from 'rm' is sent to 'error_log.txt' |
| 73 | + ``` |
| 74 | + |
| 75 | +You can _append_ standard error to a file with `2>>`. |
| 76 | + |
| 77 | +### Additional Redirections |
| 78 | + |
| 79 | +There are also redirections that can merge both `stdin` and `stderr`. See the [manual][manual] for details. |
| 80 | + |
| 81 | +## `/dev/null` |
| 82 | + |
| 83 | +`/dev/null` is a special file that discards all data written to it. |
| 84 | +It is sometimes referred to as "the bitbucket". |
| 85 | +It is useful for suppressing output or errors. |
| 86 | + |
| 87 | +```bash |
| 88 | +some_command > /dev/null # Discards stdout |
| 89 | +some_command 2> /dev/null # Discards stderr |
| 90 | +``` |
| 91 | + |
| 92 | +## Combining redirections |
| 93 | + |
| 94 | +Multiple redirections can be given at once. |
| 95 | + |
| 96 | +```bash |
| 97 | +some_command < input.txt > output.txt 2> error.txt |
| 98 | +``` |
| 99 | + |
| 100 | +Redirections can be copied. |
| 101 | + |
| 102 | +```bash |
| 103 | +# send stderr to the same destination as stdout |
| 104 | +some_command > output.txt 2>&1 |
| 105 | +``` |
| 106 | + |
| 107 | +When you see `2>&1`, read that like "redirect stderr to _whatever stdout is **currently** pointing to_". |
| 108 | +Bash processes redirections strictly from left to right, which means you can do stuff like: |
| 109 | + |
| 110 | +```bash |
| 111 | +# redirect stdout to 'output1.txt' |
| 112 | +# redirect stderr to the same place |
| 113 | +# then change stdout to 'output2.txt' |
| 114 | +some_command > output1.txt 2>&1 > output2.txt |
| 115 | +``` |
| 116 | + |
| 117 | +~~~~exercism/advanced |
| 118 | +## File Descriptors |
| 119 | +
|
| 120 | +In the context of shell programming and operating systems like Linux/Unix, a file descriptor (FD) is a non-negative integer that serves as an identifier or handle for an open file or I/O resource. Think of it like a label or a reference number that the operating system uses to keep track of open files and data streams. |
| 121 | +
|
| 122 | +The numbers you see on the left-hand side of redirections are file descriptors. |
| 123 | +
|
| 124 | +The standard I/O streams are given the first 3 file descriptors: |
| 125 | +
|
| 126 | +* standard input is FD 0 |
| 127 | +* standard output is FD 1 |
| 128 | +* standard error is FD 2 |
| 129 | +
|
| 130 | +New file descriptors can be set or created with the `exec` command and redirection operators. |
| 131 | +
|
| 132 | +```bash |
| 133 | +# create FD 3 as a copy of stdout |
| 134 | +exec 3>&1 |
| 135 | +# send stdout to a log file |
| 136 | +exec > log.txt |
| 137 | +
|
| 138 | +# then redirect both stdout and file descriptor 3 |
| 139 | +echo "some debug message" >&3 # displayed in the terminal |
| 140 | +some_command 2>&1 # stdout and stderr send to 'log.txt' |
| 141 | +``` |
| 142 | +
|
| 143 | +This technique looks quite convoluted, but it can be very useful in situations where your script is being run by some other software package, and standard output is automatically captured by the software. |
| 144 | +The `bats` command works like this. |
| 145 | +~~~~ |
| 146 | + |
| 147 | +## In Summary |
| 148 | + |
| 149 | +Why are redirections useful? |
| 150 | + |
| 151 | +* Saving Output: Store the results of a command for later review or processing. |
| 152 | +* Logging: Capture error messages for debugging. |
| 153 | +* Automation: Chain commands together, using the output of one as the input of another. |
| 154 | +* Filtering: Discard unwanted output. |
| 155 | +* Batch Processing: Process data from files instead of typing it manually. |
| 156 | +* Scripting: Redirections are essential for writing robust shell scripts. |
| 157 | + |
| 158 | + |
| 159 | +Shell redirections give you fine-grained control over how programs interact with data, enabling you to build complex workflows and manage information effectively. |
| 160 | + |
| 161 | +[pipelines]: https://exercism.org/tracks/bash/concepts/pipelines |
| 162 | +[manual]: https://www.gnu.org/software/bash/manual/bash.html#Redirections |
0 commit comments