Fixed some minor grammatical errors and styling (#23039)

* Fixed some minor grammatical errors and styling

Added example boxes that are consistent with other documents in this series of bash commands.

* Add syntax highlighting, Fixed formatting
This commit is contained in:
Johnathan Milley
2018-11-16 06:12:41 -03:30
committed by Manish Giri
parent fda88b8084
commit 7e2c6a6767

View File

@ -3,20 +3,28 @@ title: Bash Redirection
---
## Bash Redirection
One of the most powerful features of bash (and other shells that support similar syntax) is output and input redirection via pipes and concatination symbols. To start simply the problem of appending a sting to a file is considered. One could open the file in a terminal based editor such as Vim or Nano and manually paste the line in, though this is a bit tedious. Further more, what if one wanted to take the output of one command, say ' $echo 'hi' ' and put it into a file? Again, this could be done via a copy and paste, though this would get progressively more tedious if it had to be done repeatedly and this is not scriptable or automateable so insead something like the '>' or '>>' symbols can be used. With the previous examlpe this may look like:
One of the most powerful features of bash (and other shells that support similar syntax) is output and input redirection via pipes and concatenation symbols. To start simply, the problem of appending a sting to a file is considered. One could open the file in a terminal based editor such as Vim or Nano and manually paste the line in, though this is a bit tedious. Furthermore, what if one wanted to take the output of one command, say `$echo 'hi'` and put it into a file? Again, this could be done via a copy and paste, though this would get progressively more tedious if it had to be done repeatedly and this is not scriptable or automateable so insead the `>` or `>>` symbols can be used. With the previous example this may look like:
```shell
echo 'hi' > output.txt
```
or
```shell
echo 'hi' >> output.txt
```
The difference between the two is a single '>' will overwrite the destination file with the input while a '>>' will append the input to the destinion file. Interestingly, the destination need not be a file for example say you had a simple program 'add' which takes two, comma delimited integers as input from the user and output the sum. Normally runnig 'add' would wait for input from the user though using a file that contains valid input this could be automated. Say "input.txt" is a text file containing "1,2" running:
The difference between the two is a single `>` will overwrite the destination file with the input while a `>>` will append the input to the destinion file. Interestingly, the destination need not be a file for example say you had a simple program 'add' which takes two, comma delimited integers as input from the user and output the sum. Normally runnig 'add' would wait for input from the user though using a file that contains valid input this could be automated. Say "input.txt" is a text file containing "1,2" running:
```shell
input.txt > add
```
should output '3'
should output '3'.
Finally, and most powerful of all, is the pipe "|" which can be used to take a running programs output and 'pipe' it into another program. For example:
Finally, and most powerful of all, is the pipe `|` which can be used to take a running program's output and 'pipe' it into another program. For example:
```shell
sort textfile.txt | uniq
```
will first sort each line in the file alpahabetically and then print only the unique entries. This is only scratching the surface of the massive power of pipes though.