วิธีรับรายการประวัติเพื่อแสดงอย่างถูกต้องในหลายบรรทัด


15

สมมติว่าฉันได้เข้าฟังก์ชั่นที่ bash prompt, ในหลาย ๆ บรรทัดแทนที่จะบีบมันลงบนอันเดียวด้วย semicolons:

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

จะให้แสดงด้วยอักขระบรรทัดใหม่จริงแทนที่จะเป็น '\ n' ได้อย่างไร

คำตอบ:


21

คุณสามารถเปิดใช้งานและปิดใช้งานคุณลักษณะนี้ภายใน Bash โดยใช้shoptคำสั่ง จากหน้า Bash man

สิ่งที่สกัดมา

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.

เปิดใช้งานคุณสมบัติ

$ shopt -s cmdhist
$ shopt -s lithist

ปิดใช้งานคุณสมบัติ

$ shopt -u cmdhist
$ shopt -u lithist

ตัวอย่าง

$ shopt -s cmdhist
$ shopt -s lithist

ตอนนี้เมื่อฉันทำงาน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
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.