Eight little-known Bash options

Original author: Ian Miell
  • Transfer
  • Tutorial
Some Bash options are well known and often used. For example, many write at the beginning of a script

set -o xtrace

for debugging,

set -o errexit

to exit by mistake or

set -o errunset

to exit if the called variable is not set.

But there are many other options. Sometimes they are too confused described in mans, so I have collected here some of the most useful, with an explanation.

Note: Mac may have an older version of bash (3.x, not 4.x), where not all of these options are available. In this case, look here or here .

setor shopt?

There are two ways to set bash parameters: from a script or from the command line. You can use the built-in commands setand shopt. Both change the behavior of the shell, do much the same thing (with different arguments), and differ in their origin . Parameters setare inherited or borrowed from parameters of other shells, while parameters are shoptcreated in bash.

If you want to look at the current options, run:

$ set -o
$ shopt

To activate option c, setuse the long or short syntax:

$ set -o errunset
$ set -e

The effect is the same.

To disable the option, you need to put a plus instead of a minus:

$ set +e

For a long time I could not remember this syntax, because the logic seems to be wrong (minus turns on the option, and plus turns it off).

In shoptto enable and disable options, the (more logical) flags -s(set) and -u(unset) are used:

$ shopt -s cdspell # <= on
$ shopt -u cdspell # <= off

Directory Changes

There are several options that help you work with directories.

1. cdspell

With this setting, bash will begin to understand typos and will transfer you to a folder whose name you typed with an error.

$ shopt -s cdspell
$ mkdir abcdefg
$ cd abcdeg
$ cd ..

I used this option for many years, and very rarely (maybe once a year) it makes a very strange decision. But on the other days, it cdspellsaves time, literally every day.

2. autocd

If you are not ready to put up with the inefficiency of repeated input cd, you can set this option to move to the X folder if the X command does not exist.

$ shopt -s autocd
$ abcdefg
$ cd ..

In combination with autocomplete, this allows you to quickly jump to folders:

$ ./abc[TAB][RETURN]
cd -- ./abcdefg

Just don’t name the folder rm -rf *(yes, by the way, this is possible).

3. direxpand

This is a cool option that extends environment variables by pressing Tab:

$ shopt -s direxpand
$ ./[TAB]     # заменяется на...
$ /full/path/to/current_working_folder
$ ~/[TAB]     # заменяется на...
$ /full/path/to/home/folder
$ $HOME/[TAB] #  заменяется на...
$ /full/path/to/home/folder

Clean exit

4. checkjobs

This option stops the exit from the session if jobs are still running in the background.

Instead of exiting, a list of incomplete tasks is displayed. If you still want to exit, then enter again exit.

$ shopt -s checkjobs
$ echo $$
68125             # <= ID процесса для оболочки
$ sleep 999 &
$ exit
There are running jobs.
[1]+  Running                 sleep 999 &
$ echo $$
68125             # <= ID процесса для оболочки тот же
$ exit
There are running jobs.
[1]+  Running                 sleep 999 &
$ exit
$ echo $$
$ 59316           # <= на этот раз ID процесса  изменился

Substitution Abilities

5. globstar

This option gives you supernormal substitution abilities! If you enter:

$ shopt -s globstar
$ ls **

then the shell will show recursively all directories and subdirectories.

In combination with, direxpandyou can quickly see everything below in the hierarchy:

$ shopt -s direxpand
$ ls **[TAB][TAB]
Display all 2033 possibilities? (y or n) 


This option activates functions that are more often associated with regular expressions. This is sometimes very useful:

$ shopt -s extglob
$ touch afile bfile cfile
$ ls
afile bfile cfile
$ ls ?(a*|b*)
afile bfile
$ ls !(a*|b*)

Here, the patterns are placed in parentheses and separated by a vertical bar. Here are the available operators:

? = matches zero or one occurrence of the given patterns
! = show everything that does not match the given patterns
* = zero or more occurrences
+ = one or more occurrences
@ = exactly one occurrence

Accident protection

7. histverify

At first, it can be a little scary to use the use of quick start commands from the history of abbreviations !!and !$.

The option histverifyallows you to first see how Bash interprets the command before it actually starts:

$ shopt -s histverify
$ echo !$          # <= По нажатию Enter команда не запускается
$ echo histverify  # <= Она сначала демонстрируется на экране,
histverify         # <= а потом запускается 

8. Noclobber

Again, to protect against accidents, namely from overwriting a file that already exists with the redirect operator ( >). This can be a disaster if you do not have a backup.

Option set -Сprohibits such overwriting. If necessary, you can bypass the protection using the operator >|:

$ touch afile
$ set -C
$ echo something > afile
-bash: afile: cannot overwrite existing file
$ echo something >| afile

Also popular now: