首页 > 解决方案 > Bash 历史 - 多行字符串导致多行历史

问题描述

我更喜欢将所有 shell 命令放在一行中,所以~/.bash_history我确保在.cmdhistshopt -s cmdhist~/.bashrc

这可以正常工作:

$ for x in 1 2; do
> echo "$x"
> done
1
2
$ history -a
$ tail -n2  ~/.bash_history
for x in 1 2; do echo "$x"; done
history -a

但是,当输入多行字符串时,它们似乎总是被添加到历史记录中的多行:

$ echo "3
> 4"
3
4
$ echo '5
> 6'
5
6
$ history -a
$ tail -n5 ~/.bash_history
echo "3
4"
echo '5
6'
history -a

这往往会弄乱解析~/.bash_history文件和CTRL+R搜索的脚本(例如:搜索6find6'而不是完整的命令)

有没有办法调整这种行为?它是按预期工作还是一个错误?

重击版本:GNU bash v5.0.17(1)-release

标签: bash

解决方案


推荐阅读