The shopt built-in
I recently posted some Bash tips, a couple of which used the shopt built-in command. The shopt, or shell optional, built-in is interesting. It allows you to configure some additional, optional, shell behavior. You can use the command for the current session or you can use it in the ~/.bashrc file to configure options for every session. Let’s take a quick look at how to use the shopt built-in and some of the available options you can try.
Running the shopt command without any flags gives you a list of the currently configured shell options, some of which are on by default.
$ shoptautocd offcdable_vars offcdspell offcheckhash offcheckjobs offcheckwinsize on. . .To see only the set options you can run shopt with the -s flag and for unset options use the -u flag.
$ shopt -scheckwinsize oncmdhist oncomplete_fullquote onexpand_aliases onextglob onextquote onforce_fignore onhistappend oninteractive_comments onprogcomp onpromptvars onsourcepath onYou also use the -s and -u followed by a specific option to set or unset a shell option, for example to turn on autocd, which we discussed in the last post.
$ shopt -s autocdWhat shell options are there?
Let’s quickly look at some of the available shell options you might be interested in trying out.
cdable
Similar to autocd, if you enable cdable, then any argument to the cd command, that is not the name of a directory, is assumed to be the name of a variable whose value is the directory to change to.
checkjobs
If the checkjobs option is enabled then Bash will list the state of any stopped or running jobs before exiting an interactive shell.
$ top &[1] 9071$ exitexitThere are stopped jobs.[1]+ Stopped top$ exitIf any jobs are running it’ll require two exit commands in sequence to exit the interactive shell. If any jobs are stopped, like in our example above, then you can see Bash has required a second exit, after stopping the running job to exit the session.
dirspell and direxpand
Like cdspell, the dirspell option attempts spelling correction on directory names during word completion if the directory name initially supplied does not exist. It’s usually set on with the direxpand option, which replaces directory names with the results of work expansion when performing filename completion, for example when using completion with the TAB key.
$ ls dirspelltest.txt$ cat dirspall/test # hit Tab# Bash will replace the spelling mistake on the command line with the fully expanded, correct, path.$ cat /tmp/dirspell/test.txtdotglob
When set, the dotglob option includes filenames beginning with a . when filename expanding.
Many of the shopt options will probably never be useful (or are on by default) but some can be quite useful on occasion and I hope you find this enlightening!