Some Bash options are well-known and commonly used. For example, many people write at the beginning of a script
set -o xtrace
for debugging,
set -o errexit
for exiting on error or
set -o errunset
for exiting if an undefined variable is called.
But there are many other options. Sometimes they are described too confusingly in the man pages, so I’ve gathered some of the most useful ones here, with explanations.
Note: on Mac, there may be an older version of bash (3.x, not 4.x) where not all of these options are available. In this case, refer to or .
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 and shopt. Both modify the shell’s behavior, doing pretty much the same thing (with different arguments), but differ in their . Options set are inherited or borrowed from options of other shells, while options shopt are created in bash.
If you want to see the current options, run:
$ set -o
$ shopt To activate an option in set you can use either long or short syntax:
$ set -o errunset
$ set -eThe effect is the same.
To disable an option, you need to use a plus instead of a minus:
$ set +eFor a long time, I couldn't remember this syntax, as the logic seems wrong (a minus enables the option, while a plus disables it).
In shopt For enabling and disabling options, (more logically) flags -s (set) and -u (unset):
$ shopt -s cdspell # <= on
$ shopt -u cdspell # <= offChanging directories
There are several options that help work with directories.
1. cdspell
With this setting, bash will start to understand typos and will move you to the folder whose name you mistyped.
$ 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 odd decision. But on other days, cdspell saves time literally every day.
2. autocd
If you are not willing to put up with the inefficiency of repeated inputs, cdyou can set this option to move to folder X if command X 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 when you press Tab:
$ shopt -s direxpand
$ ./[TAB] # replaces with...
$ /full/path/to/current_working_folder
$ ~/[TAB] # replaces with...
$ /full/path/to/home/folder
$ $HOME/[TAB] # replaces with...
$ /full/path/to/home/folderClean output
4. checkjobs
This parameter prevents exiting the session if there are background jobs still running.
Instead of exiting, a list of unfinished jobs is displayed. If you still want to exit, type again sigreturn.
$ shopt -s checkjobs
$ echo $$
68125 # <= Process ID for the shell
$ sleep 999 &
$ exit
There are running jobs.
[1]+ Running sleep 999 &
$ echo $$
68125 # <= Process ID for the shell is the same
$ exit
There are running jobs.
[1]+ Running sleep 999 &
$ exit
$ echo $$
$ 59316 # <= this time the Process ID has changedSuper substitution abilities
5. globstar
This option gives you super substitution abilities! If you enter:
$ shopt -s globstar
$ ls **the shell will show recursively all directories and subdirectories.
In combination 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 activates features often 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*)
cfileHere, patterns are placed in parentheses and separated by a vertical bar. Here are the available operators:
? = соответствует нулю или одному вхождению заданных шаблонов ! = показать всё, что не соответствует заданным шаблонам * = ноль или более вхождений + = одно или более вхождений @ = ровно одно вхождение
Accident protection
7. histverify
At first, it may be a bit scary to use quick command launching from history via abbreviations !! and !$.
Option histverify allows you to see how Bash interprets the command before it actually executes:
$ shopt -s histverify
$ echo !$ # <= Pressing Enter doesn't execute the command
$ echo histverify # <= It is first displayed on the screen,
histverify # <= and then executed 8. Noclobber
Again, for protection against accidents, specifically against overwriting a file that already exists with the redirect operator (>). This can become catastrophic if you don't have a backup.
Option set -C prevents such overwriting. 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: habr.com
