This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| bash:cheat [2025-01-22] – dcai | bash:cheat [2025-03-21] (current) – dcai | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ===== bash cheat sheet ===== | ===== bash cheat sheet ===== | ||
| - | === IO Redirect === | + | === 💻 IO Redirect === |
| - | * '' | + | |
| - | * '' | + | |
| + | * '' | ||
| * '' | * '' | ||
| * '' | * '' | ||
| * '' | * '' | ||
| - | * '' | ||
| - | === File condition tests === | + | === 💻 File condition tests === |
| - | < | + | < |
| if [[ -f filename ]]; then | if [[ -f filename ]]; then | ||
| - | | + | |
| else | else | ||
| - | | + | |
| fi | fi | ||
| + | # OR | ||
| + | [ -f ~/ | ||
| </ | </ | ||
| - | * '' | + | * '' |
| - | * -d file: is a directory | + | * '' |
| - | * -x file: execute permission | + | * '' |
| - | * -a file: file exists | + | * '' |
| - | * -r file: have read permission | + | * '' |
| - | * -w file: have write permission | + | * '' |
| - | * -s file: file exists and is not empty | + | * '' |
| - | * file1 -nt file2: file1 is newer than file2 | + | * file1 '' |
| - | * file1 -ot file2: file1 is older than file2 | + | * file1 '' |
| - | === Condition tests === | + | === 💻 check last command status code === |
| - | < | + | < |
| - | if [[ -z $ENV_VAR ]]; then | + | if test $? = 0; then |
| - | | + | |
| else | else | ||
| - | [...] | + | |
| + | fi | ||
| + | </ | ||
| + | |||
| + | === 💻 Variable condition tests === | ||
| + | |||
| + | <code bash> | ||
| + | if [[ -z $? ]]; then | ||
| + | # last command run successfully | ||
| + | echo " | ||
| + | else | ||
| + | echo " | ||
| fi | fi | ||
| - | [ -f ~/ | ||
| - | # or | ||
| [ -z " | [ -z " | ||
| [[ ! -z " | [[ ! -z " | ||
| Line 52: | Line 63: | ||
| * '' | * '' | ||
| - | == Conditionals | + | === 💻 numberic conditionals === |
| * '' | * '' | ||
| Line 61: | Line 72: | ||
| * '' | * '' | ||
| - | === check if command available === | + | === 💻 check if command available === |
| - | < | + | < |
| if !type brew &>/ | if !type brew &>/ | ||
| echo "brew install" | echo "brew install" | ||
| Line 77: | Line 88: | ||
| </ | </ | ||
| - | === Test last command status code === | + | === 💻 Manipulating and/or expanding variables === |
| - | + | ||
| - | <code sh> | + | |
| - | if test $? = 0; then | + | |
| - | echo 'do this'; | + | |
| - | else | + | |
| - | echo 'do that'; | + | |
| - | fi | + | |
| - | </ | + | |
| - | + | ||
| - | === Manipulating and/or expanding variables === | + | |
| [[http:// | [[http:// | ||
| Line 115: | Line 116: | ||
| </ | </ | ||
| - | === Shortcuts === | + | === 💻 Shortcuts === |
| * CTRL L = Clear the screen | * CTRL L = Clear the screen | ||
| Line 127: | Line 128: | ||
| * !! = repeat last command | * !! = repeat last command | ||
| - | === For === | + | === 💻 For === |
| - | < | + | < |
| for name [in list] | for name [in list] | ||
| do | do | ||
| Line 136: | Line 137: | ||
| </ | </ | ||
| - | === Case === | + | === 💻 Case === |
| - | < | + | < |
| case expression in | case expression in | ||
| pattern1 ) | pattern1 ) | ||
| Line 147: | Line 148: | ||
| </ | </ | ||
| - | === $() vs backtick === | + | === 💻 $() vs backtick === |
| [[http:// | [[http:// | ||
| Line 153: | Line 154: | ||
| The backticks/ | The backticks/ | ||
| - | === Special bash variables === | + | === 💻 Special bash variables === |
| [[http:// | [[http:// | ||
| Line 169: | Line 170: | ||
| > The implementation of '' | > The implementation of '' | ||
| - | === Search === | + | === 💻 Search === |
| - | < | + | < |
| find . -iname " | find . -iname " | ||
| find . -type d | find . -type d | ||
| </ | </ | ||