未加星标

The shopt built-in

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二05 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

I recently postedsome Bash tips, a couple of which used the shopt built-in command. The shopt , or shell optional, built-in is interesting. It allows you to configure some additional, optional, shell behavior. You can use the command for the current session or you can use it in the ~/.bashrc file to configure options for every session. Let’s take a quick look at how to use the shopt built-in and some of the available options you can try.

TIP As an interesting side-note there are two files most often used to configure Bash shells. You use the ~/.bash_profile file to configure login shells and the ~/.bashrc file, which is used to configure interactive non-login shells. What’s a login shell? A login shell is the first process that executes under your user ID when you log in for an interactive session, for example via an SSH session. This runs the commands inside the ~/.bash_profile file (and generally some central shell configuration) which generally sets configuration like environment variables. An interactive non-login shell is launched when you do something like start a session in a terminal. This will run the commands in the ~/.bashrc file.

Running the shopt command without any flags gives you a list of the currently configured shell options, some of which are on by default.

$ shopt autocd off cdable_vars off cdspell off checkhash off checkjobs off checkwinsize on . . .

To see only the set options you can run shopt with the -s flag and for unset options use the -u flag.

$ shopt -s checkwinsize on cmdhist on complete_fullquote on expand_aliases on extglob on extquote on force_fignore on histappend on interactive_comments on progcomp on promptvars on sourcepath on

You also use the -s and -u followed by a specific option to set or unset a shell option, for example to turn on autocd , which we discussed inthe last post.

$ shopt -s autocd What shell options are there?

Let’s quickly look at some of the available shell options you might be interested in trying out.

TIP The options vary from Bash version to version, so you might not have all of these.

Similar to autocd , if you enable cdable , then any argument to the cd command, that is not the name of a directory, is assumed to be the name of a variable whose value is the directory to change to.

If the checkjobs option is enabled then Bash will list the state of any stopped or running jobs before exiting an interactive shell.

$ top & [1] 9071 $ exit exit There are stopped jobs. [1]+ Stopped top $ exit

If any jobs are running it’ll require two exit commands in sequence to exit the interactive shell. If any jobs are stopped, like in our example above, then you can see Bash has required a second exit , after stopping the running job to exit the session.

dirspell and direxpand

Like cdspell , the dirspell option attempts spelling correction on directory names during word completion if the directory name initially supplied does not exist. It’s usually set on with the direxpand option, which replaces directory names with the results of work expansion when performing filename completion, for example when using completion with the TAB key.

$ ls dirspell test.txt $ cat dirspall/test # hit Tab # Bash will replace the spelling mistake on the command line with the fully expanded, correct, path. $ cat /tmp/dirspell/test.txt

When set, the dotglob option includes filenames beginning with a . when filename expanding.

Many of the shopt options will probably never be useful (or are on by default) but some can be quite useful on occasion and I hope you find this enlightening!

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

tags: shell,options,use,some
分页:12
转载请注明
本文标题:The shopt built-in
本站链接:https://www.codesec.net/view/586957.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(77)