bash环境中的字符变量$ - 是什么意思?

时间:2021-10-09 00:26:21

I've been looking through some of the .bashrc and .profile scripts that come with various Linux distros and am seeing that sometimes they check $-.

我一直在查看一些随各种Linux发行版一起提供的.bashrc和.profile脚本,并且看到有时他们检查$ - 。

Here's one in Ubuntu

这是Ubuntu中的一个

case $- in
    *i*) ;;
    *) return;;
esac

In this case it's checking for the "i" flag is present to see if the current shell is an interactive one.

在这种情况下,它检查“i”标志是否存在以查看当前shell是否是交互式shell。

My current session gives me this :

我当前的会议给了我这个:

# echo $-
himBH

What are the other flags/options mean? Is there a comprehensive list somewhere?

其他标志/选项是什么意思?某处有完整的清单吗?

2 个解决方案

#1


12  

From man bash:

来自man bash:

-

Expands to the current option flags as specified upon invocation, by the set builtin command, or those set by the shell itself (such as the -i option).

扩展为调用时指定的当前选项标志,set builtin命令或shell本身设置的那些(例如-i选项)。

So these are the current options that control the behavior of the shell. In particular:

所以这些是控制shell行为的当前选项。尤其是:

  • h: Cache location of binaries in the $PATH. Speeds up execution, but fails if you move binaries around during the shell session.
  • h:在$ PATH中缓存二进制文件的位置。加快执行速度,但如果在shell会话期间移动二进制文件,则会失败。

  • i: The current shell is interactive
  • i:当前的shell是交互式的

  • m: Job control is enabled
  • m:启用作业控制

  • B: Brace expansion is enabled
  • B:启用支撑扩展

  • H: History substitution like !-1
  • H:历史替代就像!-1

#2


1  

They mean various things. Each letter corresponds to an option being set for bash. eg, "i" means that the shell is interactive (so the code sample you gave is a test to see if it's an interactive shell or not).

它们意味着各种各样。每个字母对应一个为bash设置的选项。例如,“i”表示shell是交互式的(因此您提供的代码示例是测试它是否是交互式shell)。

A full list is available in the bash man page. Look for "set" - here's the first line:

bash手册页中提供了完整列表。寻找“设置” - 这是第一行:

set [+abefhkmnptuvxBCEHPT] [+o option-name] [arg ...]

#1


12  

From man bash:

来自man bash:

-

Expands to the current option flags as specified upon invocation, by the set builtin command, or those set by the shell itself (such as the -i option).

扩展为调用时指定的当前选项标志,set builtin命令或shell本身设置的那些(例如-i选项)。

So these are the current options that control the behavior of the shell. In particular:

所以这些是控制shell行为的当前选项。尤其是:

  • h: Cache location of binaries in the $PATH. Speeds up execution, but fails if you move binaries around during the shell session.
  • h:在$ PATH中缓存二进制文件的位置。加快执行速度,但如果在shell会话期间移动二进制文件,则会失败。

  • i: The current shell is interactive
  • i:当前的shell是交互式的

  • m: Job control is enabled
  • m:启用作业控制

  • B: Brace expansion is enabled
  • B:启用支撑扩展

  • H: History substitution like !-1
  • H:历史替代就像!-1

#2


1  

They mean various things. Each letter corresponds to an option being set for bash. eg, "i" means that the shell is interactive (so the code sample you gave is a test to see if it's an interactive shell or not).

它们意味着各种各样。每个字母对应一个为bash设置的选项。例如,“i”表示shell是交互式的(因此您提供的代码示例是测试它是否是交互式shell)。

A full list is available in the bash man page. Look for "set" - here's the first line:

bash手册页中提供了完整列表。寻找“设置” - 这是第一行:

set [+abefhkmnptuvxBCEHPT] [+o option-name] [arg ...]