bash Cheat Sheet
Function names and special characters
The following are all permitted in function names in addition to [A-Z0-9a-z_]:
- "+", ".", "/", ":", "=", "?", "@", "", "", "^", "_", "{", "}", "~", and even some Unicode symbols
However, for portability with POSIX or other shell implementations it is not recommended.
Shell Options (shopt)
Shell options turn features within bash on and off interactively.
shopt -s- Set the option (enable it)shopt -u- Unset the option (disable it)
Shell interactivity
cdable_vars- Allowscd HOME(assumes any non-directory is the variable name) (Recommendoff)cdspell- Attempt to fix typos in directory names on the command line (Recommendon)interactive_comments- Command lines that begin with#are comments and should be ignored. (Recommendon)promptvars- Enable variable substitutions in prompt displays (Recommendon)
Execution path
checkhash- Check whether commands found in the hash table exists before executing it. (Recommendon)
Window size
checkwinsize- If set, bash checks the window size after each command and, if necessary, updates the values ofLINESandCOLUMNS. (Recommendon)
History
cmdhist- Save command history to.bash_history(Recommend ison)histappend- When updatingbashhistory, append to the file instead of overwriting it (Recommendon)histreedit- User can edit failed history substitutions whenreadlineis used (Recommendoff)histverify- Allow additional editing of history results prior to execution usingreadline(Recommendoff)lithist- Embed newlines in history entries to ensure commands are stored as one line (Recommendon)
Completions
force_fignore- IgnoreFIGNOREexpansions even when it is the only match (Recommendon)hostcomplete- When user types@on the command line attempt to complete the hostname (Recommendoff)no_empty_cmd_completion- Do not attempt a completion for an empty line (Recommendon)progcomp- Programmable completion is enabled (Recommendon)
Glob behavior
dotglob- Files that start with.are included in pathname expansion (Recommendon)extglob- Extended pattern matching for pathname expansion is enabled (Recommendoff)failglob- Bash will produce an error when a pathname expansion matches no files (Recommendoff)nocaseglob- Pathname expansion is case-insensitive (Recommendoff)nullglob- When a pathname expansion matches no files, return an empty string instead of the pattern (Recommendon)
Shell behavior
extquote- Extended quotes will double-quote$'string'and$"string"within a$parameterexpansion. ( Recommendon. Default ison.)huponexit- If set,bashwill sendSIGHUPto all jobs when an interactive login shell exits. (Recommendoff)execfail- If set, a non-interactive shell will not exit if it cannot execute the file specified as an argument to the exec builtin command. An interactive shell does not exit if exec fails. (Recommendoff)mailwarn- Shows a message about mail on login (Recommendoff)expand_aliases- If set, aliases are expanded as described above under ALIASES. This option is enabled by default for interactive shells. (Recommendon)gnu_errfmt- Use GNU's error format for error messages (Recommendoff)extdebug- Debugging features (Recommendoff)
Language behavior
nocasematch-casestatements are case-insensitive (Recommendoff)sourcepath- When enabled, the.builtin uses the$PATHenvironment to load the file. (Recommendon. Default ison.)shift_verbose-shiftdisplays an error when shifting beyond the arguments available (Recommendon)xpg_echo-echoexpands backslash-escape sequences by default (Recommendoff)compat31- Shell 3.1 compatibility (with=~and quotes) (Recommendoff)
Shell flags
login_shell- Set bybashautomatically if this shell is a login shell (Can not change)restricted_shell- This shell is restricted. (Can not change)
Set Flags
See set cheatsheet.
Privileged Mode
In this mode, the $ENV and $BASH_ENV files are not processed, shell functions are not inherited from the
environment, and the SHELLOPTS variable, if it appears in the environment, is ignored. If the shell is started with
the effective user (group) id not equal to the real user (group) id, and the -p option is not supplied,
these actions are taken and the effective user id is set to the real user id. If the -p option is supplied at startup,
the effective user id is not reset. Turning this option off causes the effective user and group ids to be set to the
real user and group ids.
Bash Problems
${!variable}and${!variable[@]}have different semantic meanings- No (easy) way to deference an array by another variable name
- The lack of default (empty) arrays leads to errors when doing
"${__emptyArray[@]}"withset -u(and leads to ugly code like"${__emptyArray[@]+"${__emptyArray[@]}"}") readexits 0 on end of file but actually may return a string leading to issues with missing newlines causing problems; use the pattern:local finished=false; while ! $finished; do read -r line || finished=true; ...and then test the value oflinefor non-empty values and handle when$finishedbecomestrue. Source- Problems with
&&or||precedence leads to errors