如何让历史条目正确显示在多行上

ico*_*ast 16 bash command-history

假设我在 bash 提示符下在多行中输入了一个函数,而不是用分号将其压缩到一行上:

$ function blah {
  echo blah
}
$ history -1
12690  function blah {\necho blah\n}
Run Code Online (Sandbox Code Playgroud)

如何让它用真正的换行符而不是 '\n' 显示?

slm*_*slm 23

您可以使用shopt命令在 Bash 中启用和禁用此功能。来自 Bash 手册页。

摘抄

cmdhist   If set, bash attempts to save all lines of a multiple-line
          command in the same history entry.  This allows  easy  re-editing 
          of multiline commands.

lithist   If  set,  and the cmdhist option is enabled, multi-line commands 
          are saved to the history with embedded newlines rather than using 
          semicolon separators where possible.
Run Code Online (Sandbox Code Playgroud)

启用该功能

$ shopt -s cmdhist
$ shopt -s lithist
Run Code Online (Sandbox Code Playgroud)

禁用该功能

$ shopt -u cmdhist
$ shopt -u lithist
Run Code Online (Sandbox Code Playgroud)

例子

$ shopt -s cmdhist
$ shopt -s lithist
Run Code Online (Sandbox Code Playgroud)

现在当我运行时history

   70  text=$(cat<<'EOF'
hello world\
foo\bar
EOF)
   71  text=$(cat<<'EOF'
hello world\
foo\bar
EOF
)
   72  ls
   73  cd IT/
...
...
  414  shopt -s lithist 
  415  history | less
  416  function blah {
  echo blah
}
  417  history | less
Run Code Online (Sandbox Code Playgroud)