String

受 lisp 啟發的 bash 多行文件

  • November 28, 2022

我想列印一些功能的一些使用資訊。我習慣於為每一行使用echoprintf

 echo "-V, --version"
 echo "   Display the version number and copyrights of the invoked tool."
 echo "-u, --usage"
 echo "   Provides brief information on how to use this tool."
 echo "-h, --help"
 echo "   Print a description of the command-line options understood by"

最近我一直在查看列表,其中可以設置一個長的多行字元串。

(defun myfunc ()
 "-V, --version
  Display the version number and copyrights of the invoked tool.
-u, --usage
  Provides brief information on how to use this tool.
-h, --help
  Print a description of the command-line options understood by"

 (commands-here))

我想對 bash 腳本的列印文件做同樣的事情(就像在 lisp 中一樣)。我能做些什麼來實現同樣的目標?

您可以使用多行字元串:

echo '
-V, --version
  Display the version number and copyrights of the invoked tool.
-u, --usage
  Provides brief information on how to use this tool.
-h, --help
  Print a description of the command-line options understood by
'

這種格式化方式的缺點是第一個空行。您可以通過在引號後立即開始第一行文本並在最後一行文本後立即結束引號來避免這種情況,但要稍微澄清一下:

echo '-V, --version
  Display the version number and copyrights of the invoked tool.
-u, --usage
  Provides brief information on how to use this tool.
-h, --help
  Print a description of the command-line options understood by'

像這樣,使用here-doc

cat<<'EOF'
 "-V, --version
  Display the version number and copyrights of the invoked tool.
-u, --usage
  Provides brief information on how to use this tool.
-h, --help
  Print a description of the command-line options understood by"
EOF

查看man bash | less +/here-doc

引用自:https://unix.stackexchange.com/questions/726610