قالب وردپرس درنا توس
Home / Tips and Tricks / How to customize Bash Shell with shopt

How to customize Bash Shell with shopt



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

If you fine-tune your Bash-shell behavior with shopt, you can control over 50 settings. We show you how to customize your Linux system exactly the way you want it.

Shopt Built-in

The shopt built-in is part of all versions of the Bash shell, so there is no need to install anything. The number of available options in shopt has increased steadily over the years. So the older version of Bash you have, the shorter the list shopt options will be.

If something does not seem to work on your machine, check man sidebar for Bash and check that the option is available in your version of shopt.

We cover everyone shopt below. We also describe how to use it and share some examples. From there, you can check out the Bash man page or the GNU Bash Reference Manual to see if any of these options sound useful or appealing.

A few shopt options are enabled by default and form part of Bash̵

7;s standard behavior. You can activate one shopt options as a short-term change to Bash. It then returns to standard behavior when you close the shell.

But if you want a change in behavior to be available when you start a Bash shell, you can make the changes permanent.

Shop options

There are 53 shopt alternative. If you use shopt command without any options, it lists these. If we move the exit through wc command, it will count lines, words, and characters for us. Because everyone shopt the option is on its own row, the number of rows is the number of options.

We write the following:

shopt | wc

shopt |  toilet in a terminal window.

To see all the alternatives, we can guide the exit through column command to display the option names in columns, or we can lead it to less.

We write the following:

shopt | column

shopt |  column in a terminal window.

Find shopt in Linux Manual

The section that discusses shopt and its options can be found in the Bash section of the Linux manual. The Bash section is over 6000 lines long. You will find the description of shopt with a lot of scrolling, or you can just search for it in the manual.

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

man bash

to bash in a terminal window.

Press in the manual / to start a search. Type the following, and then press Enter:

assoc_expand_once

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

Beginning of shoptAlternative sections will be displayed in man window.

The manual that shows the section shopt options on the Bash man page in a terminal window.

RELATED: How to use Linux’s man Command: Hidden Secrets and Basics

Set and disable options

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

  • -s: Set or activate.
  • -u: Disabled or disabled.

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

Write the following:

shopt -s

shopt -si a terminal window.

shopt -u | column

shopt -u |  column in a terminal window.

You can use one shopt option without -s or -u commands to view the on or off state of each option.

For example, we can write the following to check the setting for histverify alternative:

shopt histverify

We can write the following to set to:

shopt -s histverify

Then we can write the following to check it again:

shopt histverify

shopt histverify in a terminal window.

The histverify the option changes how an aspect of history the command works. Usually, if you ask history to repeat a command by referring to it as a number, such as !245, the command is retrieved from the command history and executed immediately.

If you prefer to review a command to make sure it is what you expected and edit it, type the following if necessary to set shopt histverify opportunity to:

!245

!  245 in a terminal window.

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

RELATED: How to use the history command on Linux

Autocd option

With 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 typed cd in front of it.

We write the following to turn on autocd alternative:

shopt -s autocd

Then we write the name of a directory:

Documents

shopt -s autocd in a terminal window.

CDspell option

When cdspell the option is enabled, Bash will automatically correct simple spelling and typing errors in directory names.

We write the following to set cdspell alternative:

shopt -s cdspell

To try to switch to a lowercase letter that should have an uppercase letter, we write the following:

cd documents

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

cd ../Picttures

shopt -s cdspell in a terminal window.

Bash changes in each directory, regardless of spelling errors.

The xpg_echo option

When xpg_echo option is enabled, the eco command will follow avoided characters, such as n for new line and t for horizontal tab.

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

shopt -s xpg_echo

We then bring n in a string we are to pass to echo:

echo "This is line onenThis is line two"

shopt -s xpg_echo in a terminal window.

The fleeting new line character forces a line break in the output.

This gives the same behavior as -e (enables interpretation of flight) echo alternative, however xpg_echo let it be the default action.

RELATED: How to use Echo Command on Linux

Dotglob option

The dotglob the option should be treated with some caution. It allows files and directories that start 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 the option ignores the dot at the beginning of their name.

First, we’ll search for files or directories ending in “geek” by typing the following:

ls *geek

A file is found and listed. Then we turn on dotglob by writing the following:

shopt -s dotglob

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

ls *geek

ls * nerd in a terminal window.

This time, two files are found and listed, one of which is a point file. You have to be careful rm and mv when you have dotglob the option is on.

The Nocaseglob option

The nocaseglob the option is similar dotglob options, except nocaseglob Causes differences in uppercase and lowercase letters in file names and directories to be ignored in name extensions.

We write the following to look for files or directories that begin with “how”:

ls how*

A file is found and listed. We write the following to turn on nocaseglob alternative:

shopt -s nocaseglob

Then we repeat ls command:

ls how*

how is it * in a terminal window.

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

Make changes permanent

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

Type the following command in your home directory to open the “.bashrc” file in the Gedit graphic text editor (or modify it to use your preferred editor):

gedit .bashrc

The gedit the editor opens with the “.bashrc” file loaded. You’ll see some shopt records are already in it.

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

You can add your own shopt options here as well. Once you have added them, save the changes and close the editor. Now, when you open a new Bash shell, your options will be set for you.

Options as far as the eye can see

It’s true shopt The command has many options, but you do not have to deal with them all at once, if ever. Since there are so many, there are probably some who will not interest you.

For example, there is a bunch that forces Bash to work in ways that are compatible with specific, older versions. It may be useful for someone, but it’s a pretty niche case.

You can view the Bash man page or the GNU Bash Reference Manual. Decide which options will make a difference for you and then experiment with them. Just be careful with options that affect how file and directory names are expanded. Try them with a benign command, like lsuntil you are comfortable with them.




Source link