This is meant to be a stupid-short introductory and reference guide for quickly understanding these Bash operators:
2>>. This is primarily done with one sentence and an example.
This guide assumes...
- ... you are using Bash terminal. (OS is not relevant)
- ... you are familiar with CLI usage and navigation.
- ... you are familiar with the concepts of Bash program arguments/flags/options.
Note: For this blog, we will assume we are only writing and reading files.
Admittedly, I never bothered to look up what the
< 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!
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).
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.
"program": This is what executables are called in terminal. Every time you
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
pbp: My alias for pasting clipboard.
⭐️ 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.
Sends stdout to a given file, instead of printing in the terminal.
$ ls -1 > list.txt $ cat list.txt lubbock.txt odessa.txt baird.txt
Adds stdout to file contents, not replacing file contents.
$ echo "Flamingo" >> favorite-birds.txt $ cat favorite-birds.txt Cardinal Penguin Flamingo
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:
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.
$ wc -w book.txt 2394 book.txt $ wc -w < book.txt 2394
If there is an error, redirect the stderr to a file instead of printing in terminal.
$ ls cuteDogPics 2> example.txt $ cat example.txt ls: cuteDogPics: No such file or directory
>, except with stderr.
$ 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 of the program left of the
| as stdin to the program to the right.
$ ls -1 buffalo-gap.txt abilene.txt clyde.txt lubbock.txt $ ls -1 | head -2 buffalo-gap.txt abilene.txt
|, 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.
Hope this helped someone, or sparked someone's interest to play around with these operators and research further on their own!
Happy Coding! 🍻