How to Customize the Bash Shell With shopt

A terminal prompt on a Linux laptop.Fatmawati Achmad Zaenuri / Shutterstock

If you fine-tune the behavior of your Bash shell with shopt, you can control over 50 parameters. We’ll show you how to customize your Linux system the way you like it.

The integrated shopt

The integrated shopt is part of all versions of the Bash shellso there is no need to install anything. The number of options available in shopt has grown steadily over the years. Thus, the older the version of Bash, the shorter the list of shopt options will be.

If something doesn’t seem to work on your computer, check the man page entry for Bash and verify that this option is available in your version of shopt.

We cover all purchase options below. We also describe how to use it and share some examples. From there you can check out the Bash man page or GNU Bash Reference Manual to see if any of these options seem useful or attractive.

Some shopt options are enabled by default and are part of the default behavior of Bash. You can enable a shopt option as a short-term modification of Bash. It will then revert to the default behavior when you close the shell.

However, if you want a changed behavior to be available every time you launch a Bash shell, you can make the changes permanent.

The shopt options

There are 53 purchase options. If you use the shopt command without any options, it lists them. If we route the output through the wc command, this count lines, words and characters for us. Since each shopt option is on its own line, the number of lines is the number of options.

We type the following:

shopt | bathroom

shopt | wc in a terminal window.

To see all the options we can direct the output through the column command to display the names of the options in the columns, or we could reduce it.

We type the following:

shopt | column

shopt | column in a terminal window.

Find shopt in the Linux manual

The section on shopt and its options is in the Bash section of the Linux manual. The Bash section has over 6,000 lines. You can find the description of shopt with a lot of scrolling, or you can just search for it in the manual.

To do this, open the manual in the Bash section:

bash man

man bash in a terminal window.

In the manual, press / to search. Type the following, then press Enter:

assoc_expand_once

Bash section of the manual, with a search term entered at the command line in a terminal window.

The start of the shoptoption section will appear in the man window.

The manual showing the shopt options section of the Bash man page in a terminal window.

RELATED: How to use the Linux man command: hidden secrets and basics

Adjustment and deactivation options

To set and disable the shopt options, use the following commands:

-s: Set or activate.
-u: Not defined or disabled.

Since some options are enabled by default, it is also handy to check which options are enabled. You can do this with the -s and -u options without using an option name. This causes shopt to list the options on and off.

Type the following:

shopt -s

shopt -s in a terminal window.

shopt -u | column

shopt -u | column in a terminal window.

You can use a shopt option without the -s or -u commands to see the enabled or disabled state of each option.

For example, we can type the following to check the setting of the histverify option:

shopt histverify

We can type the following to activate it:

shopt -s histverify

Then we can type the following to check it again:

shopt histverify

shopt histverify in a terminal window.

The histverify option changes the behavior of one aspect of the history command. Usually, if you ask history to repeat an order by referencing it by a number, like! 245, the command is retrieved from the command history and executed immediately.

If you would rather examine an order to make sure it is the one you expected and modify it, if necessary, enter the following to enable the shopt histverify option:

! 245

! 245 in a terminal window.

The command is retrieved and presented on the command line. You can delete, edit, or run it by pressing Enter.

RELATED: How to use the history command in Linux

The autocd option

When the autocd option is enabled, if you type the name of a directory on the command line and press Enter, it will be treated as if you had typed cd in front of it.

We type the following to enable the autocd option:

shopt -s autocd

Then, we type the name of a directory:

Documents

shopt -s autocd in a terminal window.

Cdspell option

When the cdspell option is enabled, Bash will automatically correct simple misspellings and typos in directory names.

We type the following to set the cdspell option:

shopt -s cdspell

To try to change to a lowercase directory that should have an uppercase initial letter, we type the following:

cd documents

Then we can type the following to try a directory name with an extra “t” in its name:

cd ../Photos

shopt -s cdspell in a terminal window.

Bash changes in every directory, regardless of spelling mistakes.

The xpg_echo option

When the xpg_echo option is on, the echo command obeys escaped characters, such as n for a new line and t for a horizontal tab.

First, we type the following to make sure the option is set:

shopt -s xpg_echo

We then include n in a string that we’ll pass to echo:

echo “This is line one nThis is line two”

shopt -s xpg_echo in a terminal window.

The escaped newline character forces a line break in the output.

This produces the same behavior as -e (activate escape interpretation) echo, but xpg_echo allows it to be the default action.

RELATED: How to use the Echo command in Linux

The dotglob option

The dotglob option should be treated with a bit of caution. It allows files and directories beginning with a period (.) To be included in name extensions or “globbing”. These are called “dot files” or “dot directories” and are usually hidden. The dotglob option ignores the dot at the beginning of their names.

First, we’re going to search for files or directories that end in “geek” by typing the following:

ls * geek

A file is found and listed. Next, we’ll enable the dotglob option by typing the following:

shopt -s dotglob

We issue the same ls command to search for files and directories ending in “geek”:

ls * geek

ls * geek in a terminal window.

This time two files are found and listed, one of which is a dot file. You must be careful with rm and mv when the dotglob option is enabled.

The nocaseglob option

The nocaseglob option is similar to the dotglob option, except that nocaseglob ignores the differences between upper and lower case letters in file names and directories in name extensions.

We type the following to search for files or directories beginning with “comment”:

ls how *

A file is found and listed. We type the following to activate the nocaseglob option:

shopt -s nocaseglob

Then we repeat the ls command:

ls how *

Here's how * in a terminal window.

Two files are found, one of which contains uppercase letters.

Make changes permanent

The changes we made will only last until we close the current Bash shell. To make them permanent across different shell sessions, we need to add them to our “.bashrc” file.

In your home directory, type the following command to open the “.bashrc” file in the Gedit graphical text editor (or modify it accordingly to use the editor you prefer):

gedit .bashrc

The gedit editor will open with the “.bashrc” file loaded. You will see that some shopt entries are already there.

The gedit editor with .bashrc loaded in and the shopt options highlighted.

You can also add your own purchasing options here. Once you’ve added them, save your changes and close the editor. Now every time you open a new Bash shell, your options will be set for you.

Options as far as the eye can see

It’s true that the shopt command has many options, but you don’t have to deal with them all at once, if ever. Since there are so many, there are probably some that won’t interest you.

For example, there are a bunch of them that force Bash to work in a way that is compatible with specific older versions. It might be useful for someone, but it’s quite a specific case.

You can watch the Bash again man page or GNU Bash Reference Manual. Decide which options will make the difference for you, then experiment with them. Just pay attention to the options that affect the way file and directory names are expanded. Try them with a benign command, like ls, until you are comfortable with them.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.