Eight Little-Known Bash Options

Some Bash options are well known and often used. For example, many at the beginning of the script write

set -o xtrace

for debugging,

set -o raised

to exit by mistake or

set -o errunset

to exit if the called variable is not set.

But there are many other options. They are sometimes too confusingly described in manas, so I've compiled some of the more useful ones here, with explanation.

Note: Macs may be running an older version of bash (3.x, not 4.x) where not all of these options are available. In this case see here or here.

set or shopt?

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

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

$ set -o
$ shopt

To activate the option in set long or short syntax is used:

$ 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 couldn't remember this syntax because the logic seems wrong (minus turns the option on and plus turns it off).

Π’ shopt (more logical) flags are used to enable and disable options -s (set) and -u (unset):

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

Changing directories

There are several options that help you work with directories.

1.cdspell

With this setting, bash will start to understand typos and will take you to the folder you misspelled.

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

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

2.autocd

If you are not ready to put up with the inefficiency of multiple 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 ..

Combined with autocomplete, this allows you to quickly jump through folders:

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

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

3.direxpand

This is a cool option that expands environment variables on Tab press:

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

clean output

4. check jobs

This option stops the session from being logged out if there are still jobs running in the background.

Instead of exiting, a list of incomplete tasks is displayed. If you still want to exit, then re-enter 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 superpowers

5.globstar

This option gives you substitution superpowers! If you enter:

$ shopt -s globstar
$ ls **

then the shell will show all directories and subdirectories recursively.

In conjunction with direxpand you can quickly view everything below in the hierarchy:

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

6.extglob

This option enables features that are more commonly associated with regular expressions. Sometimes this is very useful:

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

Here, the patterns are enclosed in parentheses and separated by vertical bars. Here are the available operators:

? = matches zero or one occurrence of the given patterns ! = show anything that doesn't match given patterns * = zero or more occurrences + = one or more occurrences @ = exactly one occurrence

Accident protection

7.histverify

It can be a bit intimidating at first to use shortcuts to quickly run commands from history. !! ΠΈ !$.

Option histverify lets you first see how Bash interprets the command before it actually runs:

$ shopt -s histverify
$ echo !$          # <= По Π½Π°ΠΆΠ°Ρ‚ΠΈΡŽ Enter ΠΊΠΎΠΌΠ°Π½Π΄Π° Π½Π΅ запускаСтся
$ echo histverify  # <= Она сначала дСмонстрируСтся Π½Π° экранС,
histverify         # <= Π° ΠΏΠΎΡ‚ΠΎΠΌ запускаСтся 

8. Noclobber

Again, to protect against accidents, namely overwriting a file that already exists with a redirect statement (>). This can be disastrous if you don't have a backup.

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

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

Source: habr.com

Add a comment