如何在 Bash 中输出多行字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10969953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:13:20  来源:igfitidea点击:

How to output a multiline string in Bash?

bash

提问by helpermethod

How can I output a multipline string in Bash without using multiple echo calls like so:

如何在不使用多个回显调用的情况下在 Bash 中输出多行字符串,如下所示:

echo "usage: up [--level <n>| -n <levels>][--help][--version]"
echo 
echo "Report bugs to: "
echo "up home page: "

I'm looking for a portable way to do this, using only Bash builtins.

我正在寻找一种便携的方式来做到这一点,只使用 Bash 内置函数。

回答by Paused until further notice.

Here documents are often used for this purpose.

此处的文档通常用于此目的。

cat << EOF
usage: up [--level <n>| -n <levels>][--help][--version]

Report bugs to: 
up home page:
EOF

They are supported in all Bourne-derived shells including all versions of Bash.

所有 Bourne 派生的 shell 都支持它们,包括所有版本的 Bash。

回答by Chris Mohr

or you can do this:

或者你可以这样做:

echo "usage: up [--level <n>| -n <levels>][--help][--version]

Report bugs to: 
up home page: "

回答by Jorge

Inspired by the insightful answers on this page, I created a mixed approach, which I consider the simplest and more flexible one. What do you think?

受到本页上有见地的答案的启发,我创建了一种混合方法,我认为这是最简单、更灵活的方法。你怎么认为?

First, I define the usage in a variable, which allows me to reuse it in different contexts. The format is very simple, almost WYSIWYG, without the need to add any control characters. This seems reasonably portable to me (I ran it on MacOS and Ubuntu)

首先,我在一个变量中定义了用法,这允许我在不同的上下文中重用它。格式非常简单,几乎是所见即所得,无需添加任何控制字符。这对我来说似乎相当便携(我在 MacOS 和 Ubuntu 上运行它)

__usage="
Usage: $(basename 
echo "$__usage"
) [OPTIONS] Options: -l, --level <n> Something something something level -n, --nnnnn <levels> Something something something n -h, --help Something something something help -v, --version Something something something version "

Then I can simply use it as

然后我可以简单地将它用作

levelN=${2:?"--level: n is required!""${__usage}"}

or even better, when parsing parameters, I can just echo it there in a one-liner:

甚至更好的是,在解析参数时,我可以在一行中将其回显:

printf "usage: up [--level <n>| -n <levels>][--help][--version]\n\nReport bugs to: \nup home page: \n"

回答by nhahtdh

Use -eoption, then you can print new line character with \nin the string.

使用-e选项,然后您可以\n在字符串中打印换行符。

Sample(but not sure whether a good one or not)

样品(但不确定好不好)

The fun thing is that -eoption is not documented in MacOS's man pagewhile still usable. It is documented in the man page of Linux.

有趣的是,该-e选项并未记录在MacOS 的手册页中,但仍然可用。它记录在Linux手册页中

回答by Gordon Davisson

Since I recommended printfin a comment, I should probably give some examples of its usage (although for printing a usage message, I'd be more likely to use Dennis' or Chris' answers). printfis a bit more complex to use than echo. Its first argument is a format string, in which escapes (like \n) are alwaysinterpreted; it can also contain format directives starting with %, which control where and how any additional arguments are included in it. Here are two different approaches to using it for a usage message:

由于我printf在评论中推荐,我可能应该给出一些使用示例(尽管为了打印使用消息,我更有可能使用 Dennis 或 Chris 的答案)。 printf使用起来比echo. 它的第一个参数是一个格式字符串,其中总是解释转义(如\n);它还可以包含以 开头的格式指令,这些指令控制在其中包含任何其他参数的位置和方式。以下是将其用于使用消息的两种不同方法:%

First, you could include the entire message in the format string:

首先,您可以在格式字符串中包含整个消息:

printf "usage: up [--level <n>| -n <levels>][--help][--version]\n\nReport bugs to: %s\nup home page: %s\n" "$bugreport" "$homepage"

Note that unlike echo, you must include the final newline explicitly. Also, if the message happens to contain any %characters, they would have to be written as %%. If you wanted to include the bugreport and homepage addresses, they can be added quite naturally:

请注意,与 不同echo,您必须明确包含最后的换行符。此外,如果消息碰巧包含任何%字符,则必须将它们写为%%. 如果您想包含错误报告和主页地址,可以很自然地添加它们:

printf "%s\n" "usage: up [--level <n>| -n <levels>][--help][--version]" "" "Report bugs to: " "up home page: "

Second, you could just use the format string to make it print each additional argument on a separate line:

其次,您可以使用格式字符串使其在单独的行上打印每个附加参数:

printf "%s\n" "usage: up [--level <n>| -n <levels>][--help][--version]" "" "Report bugs to: $bugreport" "up home page: $homepage"

With this option, adding the bugreport and homepage addresses is fairly obvious:

使用此选项,添加错误报告和主页地址是相当明显的:

if [ some test ]; then
    cat <<- xx
        line1
        line2
xx
fi

回答by Elliptical view

Also with indented source code you can use <<-(with a trailing dash) to ignore leading tabs (but not leading spaces). For example this:

同样使用缩进的源代码,您可以使用<<-(带有尾随破折号)来忽略前导制表符(但不是前导空格)。例如这个:

line1
line2

Outputs indented text without the leading whitespace:

输出没有前导空格的缩进文本:

echo $__usage

回答by user1165471

If you use the solution from @jorge and find that everything is on the same line, make sure you enclose the variable in quotes:

如果您使用@jorge 的解决方案并发现所有内容都在同一行上,请确保将变量括在引号中:

echo "$__usage"

will print everything on one line whereas

将在一行上打印所有内容,而

##代码##

will retain the newlines.

将保留换行符。