我如何在Bash中输出一个多重字符串,而不使用多个echo调用,像这样:
echo "usage: up [--level <n>| -n <levels>][--help][--version]"
echo
echo "Report bugs to: "
echo "up home page: "
我正在寻找一种可移植的方法来做到这一点,只使用Bash内置程序。
我如何在Bash中输出一个多重字符串,而不使用多个echo调用,像这样:
echo "usage: up [--level <n>| -n <levels>][--help][--version]"
echo
echo "Report bugs to: "
echo "up home page: "
我正在寻找一种可移植的方法来做到这一点,只使用Bash内置程序。
当前回答
You can write your
text
freely,
in a separate:
----file.
然后
echo "$(</pathto/your_multiline_file)"
其他回答
使用-e选项,则可以在字符串中打印带\n的新行字符。
例如:
echo -e "This will be the first line \nand this will be on the second line"
我通常使用内置的read命令,我认为它更灵活和直观。 它将一行内容读入一个变量,并允许与特殊shell变量IFS绑定的分词。 更多细节请参考本博客或手册页。
read -r -d '' usage <<-EOF
usage: up [--level <n>| -n <levels>][--help][--version]
Report bugs to: $report server
up home page: $HOME
EOF
echo "$usage"
以下是我的做法:
function help_text {
printf "\n\
Usage: ./cpanel-to-cc.sh [arguments] ... \n\
Examples: \n\
\t ./cpanel-to-cc.sh --client-id 123123 --api-key abc123def456 --domain example.com \n\
\t ./cpanel-to-cc.sh --client-id 123123 --tmp-dir /home/user/cpanel-to-cc \n\
\t ./cpanel-to-cc.sh --resync --domain example.com \n\
\t ./cpanel-to-cc.sh --purge \n\
\n\
Arguments: \n\
Option \t\t\t Long option \t\t\t Function \n\
-c <id> \t\t --client-id <id> \t\t Specify the SiteHost Client ID \n\
-k <key> \t\t --api-key <key> \t\t Specify the SiteHost API key with access to Cloud, Job and Server modules \n\
-d <domain> \t\t --domain <domain> \t\t The cPanel domain to migrate. If not specified we try migrate all \n\
-t <directory> \t --tmp-dir <directory> \t\t Directory to store temporary files and logs. Default is: $TMP_DIR \n\
-v \t\t\t --verbose \t\t\t Print debugging/verbose information \n\
-y \t\t\t --assume-yes \t\t\t Automatic yes to prompts. Assume \"yes\" as answer to all prompts \n\
-r \t\t\t --resync \t\t\t Use credentials stored and copy data into Container already created. \n\
-p \t\t\t --purge \t\t\t Remove any metadata stored on the the server. This removes any files in: $TMP_DIR \n\
-h \t\t\t --help \t\t\t Display this help and exit \n\
\n"
}
还有一件事,使用printf和预定义的变量(这里:msg)作为模板。
msg="First line %s
Second line %s
Third line %s
"
one='additional message for the first line'
two='2'
tri='this is the last one'
printf "$msg" "$one" "$two" "$tri"
这个^^^将按指定的顺序打印整条消息,并插入额外的vars而不是%s。
You can write your
text
freely,
in a separate:
----file.
然后
echo "$(</pathto/your_multiline_file)"