Jak uzyskać wpis historii, aby poprawnie wyświetlać się w wielu wierszach


15

Załóżmy, że wprowadziłem funkcję w wierszu polecenia bash, w wielu liniach zamiast ściskać ją za pomocą średnika:

$ function blah {
  echo blah
}
$ history -1
12690  function blah {\necho blah\n}

Jak wyświetlić to z prawdziwymi znakami nowej linii zamiast „\ n”?

Odpowiedzi:


21

Możesz włączyć i wyłączyć tę funkcję w Bash za pomocą shoptpolecenia. Ze strony podręcznika użytkownika Bash.

fragment

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.

Włącza tę funkcję

$ shopt -s cmdhist
$ shopt -s lithist

Wyłącza tę funkcję

$ shopt -u cmdhist
$ shopt -u lithist

Przykład

$ shopt -s cmdhist
$ shopt -s lithist

Teraz kiedy biegnę 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
Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.