Skip to content

Commit 08f8c9a

Browse files
committed
review suggestions
1 parent 5a3a78f commit 08f8c9a

File tree

3 files changed

+46
-26
lines changed

3 files changed

+46
-26
lines changed

concepts/redirections/.meta/config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"IsaacG",
77
"kotp"
88
],
9-
"blurb": "Redirections manipulate the input and output of a Bash program."
9+
"blurb": "Redirection manipulates the input and output of programs."
1010
}
1111

concepts/redirections/about.md

+44-24
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,106 @@
1-
# About Redirections
1+
# About Redirection
22

3-
In the world of the command-line interface (CLI), programs often interact with three standard data streams:
3+
In the world of the command-line interface, programs often interact with three standard data streams:
44

55
1. **Standard Input** (`stdin`): Where a program receives its input (typically from the keyboard).
66
1. **Standard Output** (`stdout`): Where a program sends its normal output (typically to the terminal screen).
77
1. **Standard Error** (`stderr`): Where a program sends error messages (typically to the terminal screen).
88

99
Recall that we introduced these in the [Pipelines and Command Lists][pipelines] chapter.
1010

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.
11+
~~~~exercism/note
12+
Pipelines can be considered a form of redirection: the standard output from one program is sent directly to the standard input of another.
13+
But we've already covered pipelines.
14+
This document is about redirecting to and from files.
15+
~~~~
16+
17+
Shell redirection is a powerful mechanism that allows you to change the destination of these streams.
18+
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.
19+
This provides considerable flexibility and control over how programs interact with data.
1220

1321
## Key Redirection Operators
1422

1523
Here are the most common redirection operators and what they do:
1624

1725
### `>` (Output Redirection)
1826

19-
* Purpose: Redirects the standard output (stdout) of a command to a file.
27+
* Purpose: Redirect the standard output (`stdout`) of a command to a file.
2028
* Behavior:
21-
* If the file doesn't exist, it's created.
29+
* If the file doesn't exist, it is created.
2230
* If the file exists, its contents are overwritten.
2331
* Example:
32+
2433
```bash
2534
# Sends the output of 'ls -l' to 'file_listing.txt'
2635
ls -l > file_listing.txt
2736
```
2837

2938
~~~~exercism/caution
30-
Redirections are performed **before** the command is executed.
39+
Redirection is performed **before** the command is executed.
3140
This means you cannot redirect to the same file you need to read from.
3241
3342
```bash
3443
date > date.txt # populate the file with the current date
3544
cat date.txt > date.txt # 'date.txt' is now empty!
3645
```
3746
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.
47+
In the second command of the above example,
48+
49+
1. Bash first truncates the file named in the redirection.
50+
This destroys the previous contents, leaving an empty file.
51+
2. Then bash reads the newly empty file.
52+
3. Then bash writes the contents (i.e., nothing) back to the file.
4153
~~~~
4254

4355
### `>>` (Append Output Redirection)
4456

45-
* Purpose: Appends the standard output (stdout) of a command to a file.
57+
* Purpose: Append the standard output (`stdout`) of a command to a file.
4658
* Behavior:
47-
* If the file doesn't exist, it's created.
59+
* If the file doesn't exist, it is created.
4860
* If the file exists, the output is added to the end of the file.
4961
* Example:
62+
5063
```bash
5164
# Adds "Another line" to the end of the file
5265
echo "Another line" >> file_listing.txt
5366
```
5467

5568
### `<` (Input Redirection)
5669

57-
* Purpose: Redirects the standard input (stdin) of a command from a file.
70+
* Purpose: Redirect the standard input (`stdin`) of a command from a file.
5871
* Behavior: The command reads its input from the specified file instead of the keyboard.
5972
* Example:
73+
6074
```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
75+
# 'tr' reads from 'lowercase.txt' and
76+
# output is redirected to 'uppercase.txt'
77+
tr 'a-z' 'A-Z' < lowercase.txt > uppercase.txt
6478
```
6579

6680
### `2>` (Error Redirection):
6781

68-
* Purpose: Redirects the standard error (stderr) of a command to a file.
82+
* Purpose: Redirect the standard error (`stderr`) of a command to a file.
6983
* Behavior: Error messages are sent to the specified file instead of the terminal.
7084
* Example:
7185
```bash
72-
rm non_existent_file 2> error_log.txt # Error message from 'rm' is sent to 'error_log.txt'
86+
# Error message from 'rm' is sent to 'error_log.txt'
87+
rm non_existent_file 2> error_log.txt
7388
```
7489

7590
You can _append_ standard error to a file with `2>>`.
7691

7792
### Additional Redirections
7893

79-
There are also redirections that can merge both `stdin` and `stderr`. See the [manual][manual] for details.
94+
There are also redirections that can merge both `stdin` and `stderr`.
95+
See the [manual][manual] for details.
8096

8197
## `/dev/null`
8298

83-
`/dev/null` is a special file that discards all data written to it.
99+
`/dev/null` is a special file that
100+
101+
* is an empty file when you read from it, and
102+
* discards all data written to it.
103+
84104
It is sometimes referred to as "the bitbucket".
85105
It is useful for suppressing output or errors.
86106

@@ -97,15 +117,15 @@ Multiple redirections can be given at once.
97117
some_command < input.txt > output.txt 2> error.txt
98118
```
99119

100-
Redirections can be copied.
120+
A redirection can be copied.
101121

102122
```bash
103123
# send stderr to the same destination as stdout
104124
some_command > output.txt 2>&1
105125
```
106126

107127
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:
128+
Bash processes redirection instructions strictly from left to right, which means you can do:
109129

110130
```bash
111131
# redirect stdout to 'output1.txt'
@@ -117,7 +137,8 @@ some_command > output1.txt 2>&1 > output2.txt
117137
~~~~exercism/advanced
118138
## File Descriptors
119139
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.
140+
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 (Input/Output) resource.
141+
Think of it like a label or a reference number that the operating system uses to keep track of open files and data streams.
121142
122143
The numbers you see on the left-hand side of redirections are file descriptors.
123144
@@ -146,7 +167,7 @@ The `bats` command works like this.
146167

147168
## In Summary
148169

149-
Why are redirections useful?
170+
Why is redirection useful?
150171

151172
* Saving Output: Store the results of a command for later review or processing.
152173
* Logging: Capture error messages for debugging.
@@ -155,7 +176,6 @@ Why are redirections useful?
155176
* Batch Processing: Process data from files instead of typing it manually.
156177
* Scripting: Redirections are essential for writing robust shell scripts.
157178

158-
159179
Shell redirections give you fine-grained control over how programs interact with data, enabling you to build complex workflows and manage information effectively.
160180

161181
[pipelines]: https://exercism.org/tracks/bash/concepts/pipelines

concepts/redirections/links.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"url": "https://www.gnu.org/software/bash/manual/bash.html#Redirections",
4-
"description": "Redirections in the manual"
4+
"description": "Redirection in the manual"
55
},
66
{
77
"url": "https://mywiki.wooledge.org/BashGuide/InputAndOutput#File_Descriptors",

0 commit comments

Comments
 (0)