Bash历史 - 记得逃脱的新行

时间:2014-06-14 13:03:53

标签: bash readline

我有很长的管道命令链,我想在多行上格式化(并使用" fc&#34进行进一步编辑;)

如何记住bash历史记录是否记得为格式化添加了新的换行符?

例如,如果我输入:

$ echo a b c \
>  | grep a
a b c

历史记录回忆起已删除的新线:

$ echo a b c  | grep a

我知道我可以使用shopt -s lithist来显示未转义的新行: 例如,运行后:

$ shopt -s lithist

以下命令:

$ for X in a b c; do
> echo $X
> done
a
b
c

被召回为:

$ for X in a b c; do
echo $X
done

但是这并不保留转义换行符的格式(格式化管道链所需的格式)。

1 个答案:

答案 0 :(得分:1)

我不认为你想要什么是完全可能的。 bash手册页说明:

If a \<newline> pair appears, and the backslash is not
itself quoted, the \<newline> is treated as a line continuation (that
is, it is removed from the input stream and effectively ignored).

使用lithist shell选项,只需重新格式化代码即可解决此问题。所以不要这样:

$ echo a b c \
>  | grep a
a b c

这样做:

$ echo a b c |
> grep a

你实际上并不需要放在行的末尾 - 如果你可以显示bash表示输入是预期的,那么它无论如何都会等待下一行。 (您可能已经知道这一点,但是)这也适用于多行输入:

$ echo 'one
> two
> three'
one
two
three

编辑:以回应下面通过评论提出的问题,了解如何在不同的行上传递多个命令行选项。

我能想到的唯一方法是预先在数组中定义选项,然后将其作为命令行参数传递。例如:

$ opts=(
> -s 'test'
> someone@example.com
> )
$ mutt ${opts[*]}  # launches: mutt -s someone@example.com
相关问题