You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: concepts/redirections/about.md
+44-24
Original file line number
Diff line number
Diff line change
@@ -1,86 +1,106 @@
1
-
# About Redirections
1
+
# About Redirection
2
2
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:
4
4
5
5
1.**Standard Input** (`stdin`): Where a program receives its input (typically from the keyboard).
6
6
1.**Standard Output** (`stdout`): Where a program sends its normal output (typically to the terminal screen).
7
7
1.**Standard Error** (`stderr`): Where a program sends error messages (typically to the terminal screen).
8
8
9
9
Recall that we introduced these in the [Pipelines and Command Lists][pipelines] chapter.
10
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.
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.
12
20
13
21
## Key Redirection Operators
14
22
15
23
Here are the most common redirection operators and what they do:
16
24
17
25
### `>` (Output Redirection)
18
26
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.
20
28
* Behavior:
21
-
* If the file doesn't exist, it's created.
29
+
* If the file doesn't exist, it is created.
22
30
* If the file exists, its contents are overwritten.
23
31
* Example:
32
+
24
33
```bash
25
34
# Sends the output of 'ls -l' to 'file_listing.txt'
26
35
ls -l > file_listing.txt
27
36
```
28
37
29
38
~~~~exercism/caution
30
-
Redirections are performed **before** the command is executed.
39
+
Redirection is performed **before** the command is executed.
31
40
This means you cannot redirect to the same file you need to read from.
32
41
33
42
```bash
34
43
date > date.txt # populate the file with the current date
35
44
cat date.txt > date.txt # 'date.txt' is now empty!
36
45
```
37
46
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.
41
53
~~~~
42
54
43
55
### `>>` (Append Output Redirection)
44
56
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.
46
58
* Behavior:
47
-
* If the file doesn't exist, it's created.
59
+
* If the file doesn't exist, it is created.
48
60
* If the file exists, the output is added to the end of the file.
49
61
* Example:
62
+
50
63
```bash
51
64
# Adds "Another line" to the end of the file
52
65
echo"Another line">> file_listing.txt
53
66
```
54
67
55
68
### `<` (Input Redirection)
56
69
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.
58
71
* Behavior: The command reads its input from the specified file instead of the keyboard.
59
72
* Example:
73
+
60
74
```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
64
78
```
65
79
66
80
### `2>` (Error Redirection):
67
81
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.
69
83
* Behavior: Error messages are sent to the specified file instead of the terminal.
70
84
* Example:
71
85
```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
73
88
```
74
89
75
90
You can _append_ standard error to a file with `2>>`.
76
91
77
92
### Additional Redirections
78
93
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.
80
96
81
97
## `/dev/null`
82
98
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
+
84
104
It is sometimes referred to as "the bitbucket".
85
105
It is useful for suppressing output or errors.
86
106
@@ -97,15 +117,15 @@ Multiple redirections can be given at once.
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.
121
142
122
143
The numbers you see on the left-hand side of redirections are file descriptors.
123
144
@@ -146,7 +167,7 @@ The `bats` command works like this.
146
167
147
168
## In Summary
148
169
149
-
Why are redirections useful?
170
+
Why is redirection useful?
150
171
151
172
* Saving Output: Store the results of a command for later review or processing.
152
173
* Logging: Capture error messages for debugging.
@@ -155,7 +176,6 @@ Why are redirections useful?
155
176
* Batch Processing: Process data from files instead of typing it manually.
156
177
* Scripting: Redirections are essential for writing robust shell scripts.
157
178
158
-
159
179
Shell redirections give you fine-grained control over how programs interact with data, enabling you to build complex workflows and manage information effectively.
0 commit comments