Bash options

Updated 29 October 2024

Options are settings that change shell or script behaviour. The set builtin command can be use to set options. There are two ways to set options either by set -o option-name or, in short form, set -option-abbrev . To disable an option, use set +o option-name or set +option-abbrev . To enable option on a script on the command line use, set +o option-name script-name or set option-abbrev script-name
AbbreviationNameEffect
-Bbrace expansionEnable brace expansion (default setting = on)
+Bbrace expansionDisable brace expansion
-CnoclobberPrevent overwriting of files by redirection (but this may be overridden by >| )
-D(none)List double-quoted strings prefixed by $, but do not execute commands in script
-aallexportExport all defined variables
-bnotifyNotify when jobs running in background terminate (not of much use in a script)
-c …(none)Read commands from ...
checkjobsInforms user of any open jobs upon shell exit. Introduced in version 4 of Bash, and still "experimental." Usage: shopt -s checkjobs (Caution: may hang!)
-eerrexitAbort script at first error, when a command exits with non-zero status (except in until or while loops, if-tests, list constructs)
-fnoglobDisable filename expansion (globbing)
globstarglobbing star-matchEnables the ** globbing operator. Usage: shopt -s globstar
-iinteractiveRuns a script in interactive mode
-nnoexecRead commands in script, but do not execute them (syntax check)
-o Option-Name(none)Invoke the Option-Name option
-o posixPOSIXChange the behavior of Bash, or invoked script, to conform to POSIX standard.
-o pipefailpipe failureCauses a pipeline to return the exit status of the last command in the pipe that returned a non-zero return value.
-pprivilegedScript runs as "suid" (caution!)
-rrestrictedScript runs in restricted mode
-sstdinRead commands from stdin
-tonecmdExit after first command
-unounsetAttempt to use undefined variable outputs error message, and forces an exit
-vverbosePrint each command to stdout before executing it
-xxtraceSimilar to -v, but expands commands
-(none)End of options flag. All other arguments are positional parameters.
--(none)Unset positional parameters. If arguments given (-- arg1 arg2), positional parameters set to arguments.

Additional bash options

Description

The shopt builtin command allows you to change additional shell optional behavior.

shopt options

OptionDescription
-oRestrict the values of <OPTNAME…> to only those also known by the set builtin
-pPrint all shell options and their current value. Default.
-qQuiet mode. Set exit code if named option is set. For multiple options: TRUE if all options are set, FALSE otherwise
-sEnable (set) the shell options named by <OPTNAME…> or list all enabled options if no names are given
-uDisabe (unset) the shell options named by <OPTNAME…> or list all disabled options if no names are given

shopt syntax

shopt [-pqsu] [-o] <OPTNAME...>
According to the option table, if only -s or -u are specified without any option names, only the currently enabled (-s) or disabled (-u) options are printed.

Exit code

When listing options, the exit code is TRUE (0), if all options are enabled, FALSE otherwise.

When setting/unsetting an option, the exit code is TRUE unless the named option doesn't exitst.

bash
shopt -s nullglob  #Example to enable nullglob option

List of additional shell options

Option nameDescription
autocd I know using the cd command feels natural but that’s something I wanted to get to your attention.If set, a command name that is the name of a directory is executed as if it were the argument to the cd command.
cdspellIf set, minor errors in the spelling of a directory component in a cd command will be corrected. The errors checked for are transposed characters, a missing character, and one character too many. If a correction is found, the corrected file name is printed, and the command proceeds.
checkjobsIf set, Bash lists the status of any stopped and running jobs before exiting an interactive shell. If any jobs are running, this causes the exit to be deferred until a second exit is attempted without an intervening command. The shell always postpones exiting if any jobs are stopped.
dirspellIf set, Bash will perform spelling corrections on directory names to match a glob.
dotglobIf set, Bash includes filenames beginning with a . (dot) in the results of pathname expansion.
nullglobIf set, Bash allows filename patterns which match no files to expand to a null string, rather than themselves.
failglobIf set, patterns which fail to match filenames during filename expansion result in an expansion error.
nocaseglobIf set, Bash matches filenames in a case-insensitive fashion when performing filename expansion.
globstarIf set, the pattern ‘**’ used in a filename expansion context will match all files and zero or more directories and subdirectories. If the pattern is followed by a ‘/’, only directories and subdirectories match.

Spotted a mistake or want something added? Send me a note.