Autosave: 2023-01-26 16:02:11
This commit is contained in:
parent
ce7ef719e2
commit
d828c70667
2 changed files with 11 additions and 3 deletions
|
@ -15,6 +15,6 @@ File descriptors are shorthand for `stdin`, `stdout` and `stderr`:
|
|||
| 1 | Standard output | `stdout` |
|
||||
| 2 | Standard error | `stderr` |
|
||||
|
||||
They are typically used when you want to redirect the output of the standard/input /output/error somewhere, e.g a file or as input to another program. A classic case is `[some_command] > /dev/null 2>&1`
|
||||
They are typically used when you want to [redirect](/Programming_Languages/Shell/Redirection.md) the output of the standard/input /output/error somewhere, e.g a file or as input to another program. A classic case is `[some_command] > /dev/null 2>&1`
|
||||
|
||||
They are all technically "files" which are open and which append themselves to every process that can run in the shell. For any command or program that you run, you will be able to access `0`, `1` and `2` for them. In this way they are similar to variables like [`$0`](/Programming_Languages/Shell/Passing_arguments_to_scripts.md#passing-arguments) and [`$@`](/Programming_Languages/Shell/Passing_arguments_to_scripts.md#passing-arguments). They have a universal meaning within the context of the shell runtime.
|
||||
|
|
|
@ -8,7 +8,8 @@ tags:
|
|||
# Redirection
|
||||
|
||||
## Redirecting outputs
|
||||
The symbol `>` is called the **redirection operator** because it redirects the output of a command to another location. You most frequently use this when you want to save contents to a file rather than standard output.
|
||||
|
||||
The symbol `>` is called the **redirection operator** because it redirects `stdout` to another program or file. You most frequently use this when you want to save contents to a file rather than standard output.
|
||||
|
||||
```bash
|
||||
ls | grep d* > result.txt
|
||||
|
@ -18,6 +19,7 @@ ls | grep d* > result.txt
|
|||
|
||||
It is common practice to combine redirection with the [file descriptors](/Programming_Languages/Shell/File_descriptors.md) to redirect the output of `stdout` and `stderr`. A common case is to [redirect error output to `/dev/null`](/Programming_Languages/Shell/Redirect_to_dev_null.md).
|
||||
|
||||
Redirection defaults to interpreting `>` as the redirection of `stdout` (`1`);
|
||||
|
||||
## Redirecting inputs
|
||||
|
||||
|
@ -27,6 +29,12 @@ We can also switch the direction of the redirection symbol and pass in a file to
|
|||
sql-processing-program < data.sql
|
||||
```
|
||||
|
||||
We can redirect a string with three carets:
|
||||
|
||||
```bash
|
||||
program <<< "this is a string input"
|
||||
```
|
||||
|
||||
## Appending
|
||||
|
||||
We use `>>` to append contents on the next available line of a pre-existing file. Continuing on from the example above:
|
||||
|
|
Loading…
Add table
Reference in a new issue