Autosave: 2022-12-11 15:00:04

This commit is contained in:
thomasabishop 2022-12-11 15:00:04 +00:00
parent c5c30ab3d4
commit 0ac3ce7a48
3 changed files with 23 additions and 3 deletions

View file

@ -0,0 +1,20 @@
---
categories:
- Programming Languages
tags:
- shell
---
# File descriptors
File descriptors are shorthand for `stdin`, `stdout` and `stderr`:
| File descriptor | Name | Common abbreviation |
| --------------- | --------------- | ------------------- |
| 0 | Standard input | `stdin` |
| 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 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.

View file

@ -16,7 +16,7 @@ You'll see the following a lot when reading shell scripts:
This is a redirection statement. It is redirecting data to the `null` device on Unix systems. Basically to a black hole or shredder where you can't access it because you don't want it to be output to stout.
The `2>&1` argument is the content: any errors that the program may generate and try to show in stout.
The `2>&1` argument is the content: any errors that the program may generate and try to show in stout. Notice we are using the [file descriptors](/Programming_Languages/Shell/File_descriptors_and_redirection.md) `1` and `2`.
## Example

View file

@ -5,12 +5,12 @@ tags:
- shell
---
## Redirection operator
# Redirection
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.
```bash
ls | grep d* >> result.txt
ls | grep d* > result.txt
```
## Appending operator