Show Menu
Cheatography

Bash Cheat Sheet (DRAFT) by

A simple bash cheat sheet

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Behavior

>&
Mix stdout and stderr
$?
Exit code of last command
type -a
Command info (shell builti­n/l­oca­tio­n/r­eserved word)
${fooBar}
Equivalent to $fooBar
${foo/­/se­arc­h/r­eplace}
Replace substring
"$foo ${foo}­"
Interp­olation
var=$(ls)
Capture stdout of ls and store in var
echo "­$nu­m42­+2"
42+2
echo "­$((­num­42+­2))­"
44 # interpret arithmetic

Control flow

IFS=","
have $foo expand using "­," instead of " "
 

Test

[ -n "­$a" ]
Nonzero length
[ -x "­$a" ]
Executable
[ -d "­$a" ]
Is directory

Misc

bash -x
Print each command with args before execution
~foo/
Home directory of user foo
find app/co­ntr­ollers/*/.rb -type f -exec wc -l {} \;
For each ruby file, list line count

Streams

buzz 1> okay.txt 2> err.txt
Redirect stdout into okay, stderr into err
2>&1
Redirect stderr into stdout
 

Cut

cut -d: -f1,2
select columns 1,2 separated by :