Stupid Short: Bash's |, >, >>, <, 2>>, 2> Operators
Tags: explainlikeimfive, tutorial, bash, linux
Published: 2019-09-14 17:18:19

---
title: Stupid Short: Bash's |, >, >>, <, 2>>, 2> Operators
published: true
description: We take an exciting, dangerous, and life-threatening journey through the magical lands of Bash's redirection operators. 
tags: explainlikeimfive, tutorial, bash, linux
cover_image: https://static.independent.co.uk/s3fs-public/thumbnails/image/2017/07/11/11/harold-0.jpg
---

This is meant to be a stupid-short introductory and reference guide for quickly understanding these Bash operators: `>`, `1>`, `>>`, `1>>`, `<`, `2>>`, `2>>`. This is primarily done with one sentence and an example.

This guide assumes...
1. ... you are using Bash terminal. (OS is not relevant)
1. ... you are familiar with CLI usage and navigation.
1. ... you are familiar with the concepts of Bash program arguments/flags/options.


## Contents
* [Mostly-Short Explanation of Fundamentals](#fundamentals)
* [Vocab for Stupid-Short List](#vocab)
* [Stupid-Short Explanations List](#meat)

> Note: For this blog, we will assume we are only writing and reading files.

_____
# Mostly-Short Fundamentals <a name="fundamentals"></a>

Admittedly, I never bothered to look up what the `|` `>` `>>` and `<` operators do in Linux until well into my programming career. If you've ever followed GitHub's instructions for adding an SSH key to your account, then you've already used are called **Redirect Operators**!

### Fundamentals of Programs
A program has three input and output streams with their corresponding number. It has a standard input (STDIN)(0), standard output (STDOUT)(1), and standard error (STDERR)(2).


![Chart of program I/O streams](https://thepracticaldev.s3.amazonaws.com/i/lamacqc2g7epx8g8hkb5.png)
> Note: My beautiful picture is only showing convention. If you build your own program, you can effectively name streams whatever you'd like and stream to whatever output. You don't know that though. If you do, act like you don't and read on.

Example: Let's take the good ol' `ls`. When you `ls` with no error, the input is `./` (Bash assumes empty is current directory), the stdout is the list of directories you then see, and the stderr is nothing (Because there's no error to catch).

Programs stream both stdout and stderr; this is because it's *not* "stdout or stderr", it's "stdout **and** stderr". Running `ls presentDir notPresentDir`, you can see both stdout and stderr.

By default, stdout, stderr, and stdin are all in the terminal. With these redirection operators, we're simply **redirecting those streams elsewhere**.

_____
# Vocab <a name="vocab"></a>
**"program"**: This is what executables are called in terminal. Every time you `cd` or `ls`, you're running a program.
**`cat`**: Program that prints the contents of a given file.
**`wc -w`**: Program that outputs the count of words in a file.
**`pbc`**: My alias for `xclip -selection clipboard`. For Mac folks, this is the exact same thing as `pbcopy`.
**`pbp`**: My alias for pasting clipboard.


_____
# Stupid-Short List <a name="meat"></a>
⭐️ The redirect operators have three numbers correlating with the output: 0 = input, 1 = output, 2 = error. If no number is given, **this defaults to 1**.
⭐️ If the file does not exist, **these operators create the file.**

### **`>`** Write stdout to given file.
Sends stdout to a given file, instead of printing in the terminal.
```bash
$ ls -1 > list.txt

$ cat list.txt
lubbock.txt
odessa.txt
baird.txt
```
_____

### **`>>`** Appends stdout to given file.
Adds stdout to file contents, not replacing file contents.
```bash
$ echo "Flamingo" >> favorite-birds.txt

$ cat favorite-birds.txt
Cardinal
Penguin
Flamingo
```
_____

### **`<`** Redirects file as stdin
Passes the contents of a file as the stdin.
Example: If we use `wc -w book.txt`, the program will output the word count and the filename of book.txt (ex: `2394 book.txt`).
If we use `wc -w < book.txt` we are "passing in" the contents of book.txt as the input, therefore `wc -w < book.txt` would return only the number (`2394`), as if we typed the whole thing into terminal.
```bash
$ wc -w book.txt
2394 book.txt

$ wc -w < book.txt
2394
```
_____

### **`2>`**: Redirect stderr to given file.
If there is an error, redirect the stderr to a file instead of printing in terminal.
```bash
$ ls cuteDogPics 2> example.txt

$ cat example.txt
ls: cuteDogPics: No such file or directory
```
_____

### **`2>>`**: Appends stderr to given file.
Exactly like `>>` and `>`, except with stderr.
```bash
$ cp ~/Documents/Reports/June2019.xlsx ~/Desktop 2>> ~/error.log

$ cat ~/error.log
cp: /Users/bananabrann/Reports/January2019.xlsx: No such file or directory
cp: /Users/bananabrann/Reports/March2019.xlsx: No such file or directory
cp: /Users/bananabrann/Reports/June2019.xlsx: No such file or directory

```
_____

### **`|`**: Use the stdout as stdin for the next program.
Use the stdout of the program left of the `|` as stdin to the program to the right.
```bash
$ ls -1
buffalo-gap.txt
abilene.txt
clyde.txt
lubbock.txt

$ ls -1 | head -2
buffalo-gap.txt
abilene.txt
```
_____

### Able to be daisy-chained

Using the `|`, we can ultimately chain as many programs as we'd like to achieve the desired result.
In the picture below, we take the stdout of the `grep` program and send it as the stdin of `head` and its arguments. We then send that stdout to the stdin of `tail`, then finally redirect the stdout to guestlist.txt.

![Alt text of image](https://thepracticaldev.s3.amazonaws.com/i/l3mekrtpir94xi7dhiun.jpeg)

_____

Hope this helped someone, or sparked someone's interest to play around with these operators and research further on their own!

Happy Coding! 🍻