Chapter 5: Text, editors and the shell environment
grep, vim and Bash cheat sheet
The chapter on one page: what to remember, the operators, patterns and keys to have at your fingertips, and flashcards for quick revision.
The chapter in eight sentences
- Every command has stdin (0), stdout (1) and stderr (2); output and errors both reach the terminal unless you redirect them.
>replaces a file,>>appends,2>captures errors,&>captures both, and/dev/nullthrows anything away.- A pipe
|feeds one command's stdout into the next command's stdin;teesaves a copy on the way. grepprints matching lines;-i,-v,-n,-c,-w,-rand-lchange what and how, and-Eenables+,?,{n,m},|and( ).- In regular expressions,
^and$anchor,.is any character,*repeats, and[…]is one character from a set. - vim starts in normal mode:
ito insert,Escto stop,:wqto save and quit,:q!to give up,uto undo. - Variables are set with
NAME=valueand read with$NAME; only exported variables reach the programs you start, andPATHdecides where commands are found. - Aliases and settings last for one shell unless you put them in
~/.bashrc(aliases, prompt, history) or~/.bash_profile(exported variables);/etc/profile.dapplies to everyone.
Cheat sheet
Redirection
| Write | Means |
|---|---|
cmd > file, cmd >> file | stdout to a file: replace, append |
cmd 2> file, cmd 2>> file | stderr to a file |
cmd &> file (> file 2>&1) | both to one file |
cmd 2> /dev/null | discard errors |
cmd < file | stdin from a file |
cmd1 | cmd2 | stdout of cmd1 into stdin of cmd2 |
cmd | tee file | cmd2 | save a copy and pass it on (-a appends) |
grep
| Write | Means |
|---|---|
grep -i, -v, -n, -c | ignore case, invert, number lines, count |
grep -w, -r, -l, -E | whole words, recurse, file names only, extended |
^, $, ^$ | start, end, empty line |
., .*, [abc], [^abc], [[:digit:]] | any char, anything, set, not in set, class |
\<, \> | start, end of a word |
-E: + ? {n,m} a|b ( ) | one or more, optional, count, either, group |
vim
| Keys | Do |
|---|---|
i a o, Esc | insert before / after / new line, back to normal |
:w, :wq, :q! | save, save and quit, quit discarding |
x, dd, yy, p | delete char, cut line, copy line, paste |
u, Ctrl+R | undo, redo |
gg, G, :N | first line, last line, line N |
/text, n | search, next match |
:%s/old/new/g | replace everywhere |
v, V, Ctrl+V | select characters, lines, block |
Environment
| Write | Means |
|---|---|
NAME=value, $NAME, ${NAME} | set, read, read next to other text |
export NAME, export NAME=value | make it an environment variable |
unset NAME, env, set | remove, list environment, list all |
NAME=value cmd | set it for one command |
export PATH=$PATH:/dir | add a directory to the command search |
alias name='cmd', unalias name | create, remove an alias |
~/.bashrc, ~/.bash_profile, /etc/profile.d/*.sh | personal per shell, personal per login, everyone |
source ~/.bashrc | apply to the current shell |
Flashcards
- The three standard streams and their numbers?
- stdin **0**, stdout **1**, stderr **2**.
- Append output to a file instead of replacing it?
- `>>`
- Hide a command's error messages?
- `cmd 2> /dev/null`
- Save a pipeline's data to a file and keep it flowing?
- `… | tee file | …`
- Show lines that do not match a pattern?
- `grep -v PATTERN`
- What do ^ and $ mean in a regular expression?
- The start and the end of the line.
- What does .* match?
- Any characters, any number of times, including none.
- Which grep option makes +, ?, | and ( ) work without backslashes?
- `-E` (extended regular expressions)
- vim is doing odd things when you type. First move?
- Press **Esc** to get back to normal mode.
- Quit vim and discard your changes?
- `:q!`
- Copy a line and paste it below in vim?
- `yy` then `p`
- Make a variable visible to programs you start?
- `export NAME`
- Where do personal aliases go?
- `~/.bashrc`
- Apply an edited ~/.bashrc to the current shell?
- `source ~/.bashrc`