Skip to content

Commit da43e56

Browse files
Add pipe to redirection chapter (bobbyiliev#41)
* add pipe to redirection chapter * Update 023-bash-redirection.md Co-authored-by: Bobby Iliev <bobby@bobbyiliev.com>
1 parent 966d49b commit da43e56

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

ebook/en/content/023-bash-redirection.md

+48-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Redirection in Bash
22

3-
A Linux superuser must have a good knowledge of redirection in Bash. It is an essential component of the system and is often helpful in the field of Linux System Administration.
3+
A Linux superuser must have a good knowledge of pipes and redirection in Bash. It is an essential component of the system and is often helpful in the field of Linux System Administration.
44

55
When you run a command like ``ls``, ``cat``, etc, you get some output on the terminal. If you write a wrong command, pass a wrong flag or an wrong command-line argument, you get error output on the terminal.
66
In both the cases, you are given some text. It may seem like "just text" to you, but the system treats this text differently. This identifier is known as a File Descriptor (fd).
@@ -11,6 +11,14 @@ In Linux, there are 3 File Descriptors, **STDIN** (0); **STDOUT** (1) and **STDE
1111
* **STDOUT** (fd: 1): Manages the output text in the terminal.
1212
* **STDERR** (fd: 2): Manages the error text in the terminal.
1313

14+
# Difference between Pipes and Redirections
15+
16+
Both *pipes* and *redidertions* redirect streams `(file descriptor)` of process being executed. The main diffrence is that *redirections* deal with `files stream`, sending the output stream to a file or sending the content of a given file to the input stream of the process.
17+
18+
On the otherhand a pipe connects two commands by sending the output stream of the first one to the input stream of the second one. without any redidertions specified.
19+
20+
# Redirection in Bash
21+
1422
## STDIN (Standard Input)
1523
When you enter some input text for a command that asks for it, you are actually entering the text to the **STDIN** file descriptor. Run the ``cat`` command without any command-line arguments.
1624
It may seem that the process has paused but in fact it's ``cat`` asking for **STDIN**. ``cat`` is a simple program and will print the text passed to **STDIN**. However, you can extend the use case by redirecting the input to the commands that take **STDIN**.
@@ -133,3 +141,42 @@ There is also a shorter way to do this.
133141
```
134142
./install_package.sh > file.txt 2>&1
135143
```
144+
145+
# Piping
146+
147+
So far we have seen how to redirect the **STDOUT**, **STDIN** and **STDOUT** to and from a file.
148+
To concatenate the output of program *(command)* as the input of another program *(command)* you can use a vertical bar `|`.
149+
150+
Example:
151+
```
152+
ls | grep ".txt"
153+
```
154+
This command will list the files in the current directory and pass output to *`grep`* command which then filter the output to only show the files that contain the string ".txt".
155+
156+
Syntax:
157+
```
158+
[time [-p]] [!] command1 [ | or |& command2 ] …
159+
```
160+
161+
You can also build arbitrary chains of commands by piping them together to achieve a powerful result.
162+
163+
This examble create a listing of every user which owns a file in a given directory as well as how many files and directories they own:
164+
```
165+
ls -l /projects/bash_scripts | tail -n +2 | sed 's/\s\s*/ /g' | cut -d ' ' -f 3 | sort | uniq -c
166+
```
167+
Output:
168+
```
169+
8 anne
170+
34 harry
171+
37 tina
172+
18 ryan
173+
```
174+
175+
## Summary
176+
|**Operator** |**Description** |
177+
|:---|:---|
178+
|`>`|`Save output to a file`|
179+
|`>>`|`Append output to a file`|
180+
|`<`|`Read input from a file`|
181+
|`2>`|`Redirect error messages`|
182+
|`\|`|`Send the output from one program as input to another program`|

0 commit comments

Comments
 (0)