📜 ⬆️ ⬇️

Eight obscure 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 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 confusingly described in manas, so I have gathered 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 .

set or shopt ?


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

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

 $ set -o $ shopt 

To activate an option in set use a 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 (the minus turns on the option, and the plus turns it off).

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

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

Changing directories


There are several options that help to work with directories.

1. cdspell


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

 $ 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 takes a very strange decision. But on the other days, cdspell saves time, literally every day.

2. autocd


If you are not ready to put up with the inefficiency of entering cd multiple times, 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 auto-completion, this allows you to quickly jump into folders:

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

Just do not call the folder rm -rf * (yes, by the way, this is possible).

3. direxpand


This is a cool option that expands 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 logout if tasks are still running in the background.

Instead of output, a list of outstanding tasks is displayed. If you still want to log out, then again type 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   

Superhuman Substitution Abilities


5. globstar


This option gives you super powers on substitution! If you enter:

 $ shopt -s globstar $ ls ** 

then the shell will show all directories and subdirectories recursively.

In combination with direxpand you can quickly view everything that is lower in the hierarchy:

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

6. extglob


This option activates functions that are more commonly associated with regular expressions. Sometimes it is very useful:

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

Here, 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 all that does not match the specified patterns
 * = zero or more occurrences
 + = one or more occurrences
 @ = exactly one entry 

Accident protection


7. histverify


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

The histverify option allows histverify to first see how Bash interprets a 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 rewriting. If necessary, you can bypass the protection with the operator >| :

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

Source: https://habr.com/ru/post/452522/


All Articles