gpt4 book ai didi

bash - 源内置和 $HISTCMD 变量

转载 作者:行者123 更新时间:2023-12-05 07:29:31 39 4
gpt4 key购买 nike

我在 bash 的“source”内置命令中遇到了关于 $HISTCMD 环境变量显示的意外行为。

我有一个 init_things 文件包含:

echo $HISTCMD

我在 bash shell 中键入以下内容:

$> echo $HISTCMD
100

$> cat init_things
echo $HISTCMD

$> source init_things
1

我希望最后一条命令显示 102。我在 zsh shell 上试​​过了;它按预期显示 102。

有没有办法让 bash 像我期望的那样运行? (没有在“init_things”中将 HISTCMD 值重置为 1)?

为什么 bash 会这样? (我在 bash 手册页中找不到答案)

它与 bash 的“交互性”有关吗?

bash version: 4.3

zsh version 5.1

最佳答案

我们对 histcmd 了解多少?

   HISTCMD
The history number, or index in the history list, of the current
command. Assignments to HISTCMD are ignored. If HISTCMD is unset,
it loses its special properties, even if it is subsequently reset.

我们还能从手册中了解到哪些 secret ?

   HISTCONTROL
A colon-separated list of values controlling how commands are saved
on the history list. If the list of values includes ignorespace,
lines which begin with a space character are not saved in the his‐
tory list. A value of ignoredups causes lines matching the previous
history entry to not be saved. A value of ignoreboth is shorthand
for ignorespace and ignoredups. A value of erasedups causes all
previous lines matching the current line to be removed from the his‐
tory list before that line is saved. Any value not in the above
list is ignored. If HISTCONTROL is unset, or does not include a
valid value, all lines read by the shell parser are saved on the
history list, subject to the value of HISTIGNORE. The second and
subsequent lines of a multi-line compound command are not tested,
and are added to the history regardless of the value of HISTCONTROL.

具体来说:

A value of ignoredups causes lines matching the previous history entry to not be saved.

这是问题的原因吗?也许……

$ echo $HISTCONTROL 
ignoredups:

...但不太可能。

我们可以在最近的 bash 中重现吗?

$ source init_things 
7615
$ . init_things
7616

不,不是真的。我们可以在 bash 4.3 中重现吗?

$ . init_things 
1

是的!

CHANGELOG 中有几行关于 HISTCMD 的内容,但都没有描述这个特定问题。我猜这只是 HISTCMD 在之前通过 source 遇到的初始化方式中的一个错误。

关于bash - 源内置和 $HISTCMD 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52705145/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com