char*
A pointer to a character, typically used to point to the start of a string. Strings in C are just arrays of characters ending with a null terminator \0. Adding a number to the pointer moves it forward by that many characters. sleep
Pauses the script for a specified number of seconds. Used to simulate delays, timeouts, or hung processes in testing. /dev/null
A special file on Linux that discards anything written to it. Commonly used to suppress unwanted output. Array Declaration ()
Parentheses are used to declare an array in bash. Each space-separated value becomes its own element, accessible by index starting at 0. "$@" can be used to populate an array with all script arguments. case ... in ... esac
Bash's switch statement, matching a value against a set of patterns. esac closes the case block (it's "case" spelled backwards). Each pattern ends with ) and each case ends with ;;. Note: *) is the catch-all pattern in a case statement, matching anything not already matched. Always placed last so specific patterns are checked first. $@
Represents all arguments passed to a script or function. Quoting it with "$@" preserves arguments that contain spaces as single units. Commonly used to pass all arguments along to another command or store them. |
# Length Operator
When used inside ${}, the # returns the length of a variable or array. For strings it returns the number of characters, for arrays the number of elements. ;
Separates two commands on the same line, running them sequentially. The second command always runs regardless of whether the first succeeded or failed. Different from && which only runs the second command if the first succeeded. exit
Exits the script with a return code. 0 means success, any non-zero value means failure. The exit code can be checked by the calling process to determine if the script succeeded. ""
Quotes preserve a value as a single unit if it contains spaces. Without quotes, spaces would split the value into separate arguments. ${}
Curly braces denote a variable expansion in bash. Used to explicitly mark the boundaries of a variable name. Required when combining a variable with other characters. |
Array Indexing
Arrays in bash start at index 0, not 1. The last element is always at index length - 1. The second to last element is at index length - 2. $(())
Performs arithmetic in bash. The result of the calculation is used as a value. >>
Appends output to a file without overwriting existing content. Use > instead to overwrite the file completely. >&2
Redirects output to stderr (standard error) instead of stdout (standard output). Used for error messages so they can be separated from normal output. :-
Provides a default value if the variable is unset or empty. The value after :- is used as a fallback. rm
Removes a file from the filesystem. Without flags it will prompt for confirmation on protected files and error on missing files. A flag forces deletion — suppresses all prompts and ignores missing files, making it safe to use in scripts where the file may or may not exist. |
Cheatography
https://cheatography.com
Bash Cheat Sheet (DRAFT) by blakecromar
A cheatsheet for bash
This is a draft cheat sheet. It is a work in progress and is not finished yet.