【问题标题】:Bash history - remember escaped new linesBash 历史 - 记住逃脱的新行
【发布时间】:2014-06-14 13:03:52
【问题描述】:

我有很长的管道命令链,我希望将它们格式化为多行(并调用“fc”进行进一步编辑)

如何让 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

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

【问题讨论】:

    标签: bash readline


    【解决方案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
    

    【讨论】:

    • 谢谢!这解决了我的大部分用例。有命令行开关有什么技巧吗,每行一个,例如: mycommand \ --name1 --value1 \ --name2 --value2 \ ...
    • 嗯......我唯一能想到的就是......好吧,我会更新答案。
    • Dern...我希望 bash 不会将这些信息丢给历史 :( 但是这个解决方法现在似乎很好 - 谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    • 1970-01-01
    • 2012-01-19
    • 2012-01-25
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    相关资源
    最近更新 更多