Skip to content

Commit a52e015

Browse files
authored
Enhance documentation for output redirection
Clarified the explanation of the '>' operator and its behavior with file redirection. Added examples and details about standard output and error handling.
1 parent 50b1c69 commit a52e015

File tree

1 file changed

+53
-10
lines changed

1 file changed

+53
-10
lines changed
Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,77 @@
1-
In this stage, you'll implement support for redirecting the output of a command to a file.
1+
In this stage, you'll implement support for redirecting a command's output to a file.
22

3-
### The `1>` Operator
3+
### The `>` Operator
44

5-
The `1>` operator is used to redirect the output of a command to a file.
6-
But, as a special case, if the file descriptor is not specified before the operator `>`, the output is redirected to the standard output by default, so `>` is equivalent to `1>`.
5+
The `>` operator redirects the standard output of a command to a file. When you use `>`, the output that would normally appear on the terminal is instead written to the specified file.
76

8-
Learn more about [Redirecting Output](https://www.gnu.org/software/bash/manual/bash.html#Redirecting-Output).
7+
If the file doesn't exist, it is created. If the file already exists, it is overwritten (replacing its old contents).
98

10-
### Tests
9+
For example:
10+
11+
```bash
12+
$ echo hello > output.txt
13+
$ cat output.txt
14+
hello
15+
```
1116

12-
The tester will execute your program like this:
17+
### File Descriptor Notation
18+
19+
In Unix, standard output is identified by the number `1`. You can explicitly write `1>` to redirect standard output:
1320

1421
```bash
15-
./your_program.sh
22+
$ echo hello 1> file.txt
1623
```
1724

18-
It will then send a series of commands to your shell, executing commands, and redirecting their output to a file:
25+
However, since redirecting standard output is so common, the shell allows you to omit the `1` and just write `>`:
26+
27+
```bash
28+
$ echo hello > file.txt
29+
```
30+
31+
Both commands do exactly the same thing. The `>` is shorthand for `1>`.
32+
33+
### Error Output Is Not Redirected
34+
35+
The `>` operator only redirects standard output, not standard error. If a command produces error messages, they still appear on the terminal:
36+
37+
```bash
38+
$ cat nonexistent > output.txt
39+
cat: nonexistent: No such file or directory
40+
$ cat output.txt
41+
$
42+
```
43+
44+
The error message appears on the terminal, and `output.txt` is empty (or contains only the non-error output if there was any).
45+
46+
### Tests
47+
48+
The tester will execute your program:
49+
```bash
50+
$ ./your_program.sh
51+
```
52+
53+
It will then send a series of commands to your shell and redirect their output to a file:
1954

2055
```bash
2156
$ ls /tmp/baz > /tmp/foo/baz.md
2257
$ cat /tmp/foo/baz.md
2358
apple
2459
blueberry
60+
2561
$ echo 'Hello James' 1> /tmp/foo/foo.md
2662
$ cat /tmp/foo/foo.md
2763
Hello James
64+
2865
$ cat /tmp/baz/blueberry nonexistent 1> /tmp/foo/quz.md
2966
cat: nonexistent: No such file or directory
3067
$ cat /tmp/foo/quz.md
3168
blueberry
3269
```
3370

34-
The tester will check if the commands are executed correctly and redirect their output to a file as specified. The file contents will also be checked for correctness.
71+
The tester will verify that:
72+
- Commands with `>` redirect their standard output to the specified file.
73+
- Commands with `1>` work identically to `>`.
74+
- Files are created if they don't exist.
75+
- Error messages still appear on the terminal (not redirected).
76+
- Only standard output is written to the file.
77+

0 commit comments

Comments
 (0)