shell shopt_如何使用shopt定制Bash Shell

shell shopt

shell shopt

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

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

如果您使用shopt微调Bash shell的行为,则可以控制50多个设置。 我们将向您展示如何按照自己的喜好定制Linux系统。

内置购物 (The shopt Built-in )

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

内置的shoptBash shell所有版本的一部分,因此无需安装任何东西。 这些年来, shopt中可用的选项数量一直在稳定增长。 因此,您拥有的Bash版本越旧, shopt选项的列表就会越短。

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

如果您的计算机似乎无法正常运行,请检查Bash的man页条目,并确认在您的shopt版本中该选项可用。

We cover all the shopt 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 whether any of those options sound useful or appealing.

我们涵盖了下面所有的shopt选项。 我们还将描述如何使用它并分享一些示例。 从那里,您可以查看Bash手册页或GNU Bash参考手册,以查看这些选项中的任何一个听起来有用还是吸引人。

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

一些小shopt选项默认情况下处于启用状态,并且是Bash默认行为的一部分。 您可以启用shopt选项作为Bash的短期更改。 关闭外壳程序后,它将恢复为默认行为。

However, if you want a modified behavior to be available whenever you launch a Bash shell, you can make the changes permanent.

但是,如果希望启动Bash Shell时可以使用修改的行为,则可以使更改永久生效。

购物选项 (The shopt Options )

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

有53个shopt场所选项。 如果使用不带任何选项的shopt命令,它将列出这些选项。 如果我们通过wc命令通过管道传递输出,它将为我们计算行数,单词数和字符数。 因为每个小shopt选项都在其自己的行上,所以行数就是选项的数量。

We type the following:

我们输入以下内容:

shopt | wc
shopt | wc in a terminal window.

To see all of the options, we can pipe the output through the column command to display the option names in columns, or we could pipe it into less.

要查看所有选项,我们可以通过column命令管道方式输出输出,以在column中显示选项名称,也可以将其以管道方式传递less

We type the following:

我们输入以下内容:

shopt | column
shopt | column in a terminal window.

在Linux手册中查找购物 (Finding shopt in the Linux Manual)

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

讨论shopt及其选项的部分位于Linux手册的Bash部分。 Bash部分的长度超过6,000行。 您可以滚动浏览找到小shopt的描述,也可以在手册中进行搜索。

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

为此,请在“重击”部分打开手册:

man bash
man bash in a terminal window.

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

在手册中,按/开始搜索。 键入以下内容,然后按Enter:

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

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

在开始shopt选项部分将出现在man窗口。

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

设置和取消设置选项 (Setting and Unsetting Options)

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

要设置和取消设置shopt选项,请使用以下命令:

  • -s: Set, or enable.

    -s :设置或启用。

  • -u: Unset, or disable.

    -u :取消设置或禁用。

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

由于某些选项默认情况下处于启用状态,因此方便查看哪些选项已启用。 您可以使用-s-u选项来执行此操作,而无需使用选项名称。 这将导致shopt者列出打开和关闭的选项。

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 on or off state for each option.

您可以使用不带-s-u命令的shopt选项来查看每个选项的打开或关闭状态。

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

例如,我们可以键入以下内容来检查histverify选项的设置:

shopt histverify

We can type the following to set it to on:

我们可以输入以下内容将其设置为on:

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 how one aspect of the history command operates. Usually, if you ask history to repeat a command by referencing it by number, like !245, the command is retrieved from the command history and executed immediately.

histverify选项histverify history命令的一方面的操作方式。 通常,如果您要求history通过按数字引用重复命令(例如!245 ,则会从命令历史记录中检索命令并立即执行。

If you prefer to review a command to make sure it’s the one you expected and edit it, if necessary, type the following to set the shopt histverify option to on:

如果您希望查看命令以确保它是您期望的命令并进行编辑,请在必要时键入以下命令以将shopt histverify选项设置为on:

!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.

该命令将被检索并显示在命令行中。 您可以按Enter删除,编辑或执行它。

autocd选项 (The autocd Option)

With the autocd option set to on, if you type the name of a directory on the command line and press Enter, it will be treated as if you’ve typed cd in front of it.

autocd选项设置为on的情况下,如果您在命令行上键入目录的名称并按Enter,则将其视为如同您在其前面键入cd一样。

We type the following to turn on the autocd option:

我们键入以下内容以打开autocd选项:

shopt -s autocd

Then, we type the name of a directory:

然后,我们输入目录的名称:

Documents
shopt -s autocd in a terminal window.

cdspell选项 (The cdspell Option)

When the cdspell option is turned on, Bash will automatically correct simple spelling mistakes and typos in directory names.

cdspell选项时,Bash将自动更正目录名称中的简单拼写错误和拼写错误。

We type the following to set the cdspell option:

我们输入以下内容来设置cdspell选项:

shopt -s cdspell

To try to change into a directory in lowercase 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:

然后,我们可以键入以下内容来尝试使用名称中带有一个额外的“ t”的目录名称:

cd ../Picttures
shopt -s cdspell in a terminal window.

Bash changes into each directory, regardless of the spelling mistakes.

无论拼写错误如何,Bash都会更改到每个目录中。

xpg_echo选项 (The xpg_echo Option)

When the xpg_echo option is set to on, the echo command will obey escaped characters, like \n for new line and \t for horizontal tab.

xpg_echo选项设置为on时,echo命令将服从转义字符,例如\n换行, \t代表水平制表符。

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

首先,我们键入以下内容以确保设置了该选项:

shopt -s xpg_echo

We then include \n in a string we’re going to pass to echo:

然后,我们在传递给echo的字符串中包含\n

echo "This is line one\nThis is line two"
shopt -s xpg_echo in a terminal window.

The escaped new-line character forces a line break in the output.

转义的换行符会在输出中强制换行。

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

这产生与-e (启用转义解释) echo选项相同的行为,但是xpg_echo允许它成为默认操作。

dotglob选项 (The dotglob Option)

The dotglob option should be treated with a bit of caution. It allows files and directories that start with a period (.) to be included in name expansions or “globbing.” These are called “dot files” or “dot directories” and they’re usually hidden. The dotglob option ignores the dot at the start of their names.

dotglob选项应谨慎处理。 它允许将以句点( . )开头的文件和目录包含在名称扩展或“ globbing”中。 这些被称为“点文件”或“点目录”,它们通常是隐藏的。 dotglob选项将忽略其名称dotglob的点。

First, we’ll do a search for files or directories that end in “geek” by typing the following:

首先,我们将通过键入以下内容来搜索以“ geek”结尾的文件或目录:

ls *geek

One file is found and listed. Then, we’ll turn on the dotglob option by typing the following:

找到一个文件并列出。 然后,通过键入以下内容来打开dotglob选项:

shopt -s dotglob

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

我们发出相同的ls命令来查找以“ 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 need to be careful with rm and mv when you’ve got the dotglob option set to on.

这次找到并列出了两个文件,其中一个是点文件。 将dotglob选项设置为on时,您需要注意rmmv

nocaseglob选项 (The nocaseglob Option)

The nocaseglob option is similar to the dotglob option, except nocaseglob causes differences in upper- and lowercase letters in file names and directories to be ignored in name expansions.

nocaseglob选项与dotglob选项相似,不同之处nocaseglob导致文件名和目录中大小写字母的差异在名称扩展中被忽略。

We type the following to look for files or directories that start with “how”:

我们键入以下内容来查找以“ how”开头的文件或目录:

ls how*

One file is found and listed. We type the following to turn on the nocaseglob option:

找到一个文件并列出。 我们键入以下内容以打开nocaseglob选项:

shopt -s nocaseglob

Then, we repeat the ls command:

然后,我们重复ls命令:

ls how*
ls how* in a terminal window.

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

找到两个文件,其中一个包含大写字母。

永久进行更改 (Making Changes Permanent)

The changes we’ve 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.

我们所做的更改只会持续到关闭当前的Bash shell为止。 为了使它们在不同的Shell会话中永久存在,我们需要将它们添加到“ .bashrc”文件中。

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

在您的主目录中,键入以下命令以在图形Gedit文本编辑器中打开“ .bashrc”文件(或相应地更改它以使用您喜欢的编辑器):

gedit .bashrc

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

gedit编辑器将打开,并加载“ .bashrc”文件。 您会看到其中已经有一些小shopt条目。

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

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

您也可以在此处添加自己的shopt选项。 添加它们后,保存更改并关闭编辑器。 现在,无论何时打开新的Bash shell,都将为您设置选项。

尽眼所见的选择 (Options as Far as the Eye Can See)

It’s true the shopt command has a lot of options, but you don’t have to come to grips with them all at once, if ever. Since there are so many, there are likely some that will be of no interest to you.

确实, shopt命令具有很多选项,但是您不必一次全部掌握它们(如果有的话)。 由于数量众多,因此可能您可能会不感兴趣。

For example, there are a bunch that force Bash to operate in ways that are compatible with specific, older versions. That might be useful for someone, but it’s a fairly niche case.

例如,有一堆迫使Bash以与特定的较旧版本兼容的方式运行。 这对某人可能有用,但这是一个相当小众的案例。

You can review the Bash man page or GNU Bash Reference Manual. Decide which options are going to make a difference for you, and then experiment with them. Just be careful with options that affect the way file and directory names are expanded. Try them with a benign command, like ls, until you’re comfortable with them.

您可以查看Bash手册页GNU Bash参考手册。 确定哪些选项对您有所帮助,然后尝试一下。 请注意影响文件和目录名称扩展方式的选项。 尝试使用像ls这样的良性命令,直到对它们感到满意为止。

翻译自: https://www.howtogeek.com/691980/how-to-customize-the-bash-shell-with-shopt/

shell shopt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值