受 lisp 启发的 bash 多行文档

受 lisp 启发的 bash 多行文档

我想打印一些功能的使用信息。我通常对每一行使用echoor 。printf

  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 中)。我可以做什么来实现同样的目标?

答案1

像这样,使用这里-文档

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

答案2

您可以使用多行字符串:

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'

相关内容