我如何在Bash中输出一个多重字符串,而不使用多个echo调用,像这样:

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

我正在寻找一种可移植的方法来做到这一点,只使用Bash内置程序。


当前回答

或者你可以这样做:

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

Report bugs to: 
up home page: "

其他回答

Since I recommended printf in 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). printf is a bit more complex to use than echo. Its first argument is a format string, in which escapes (like \n) are always interpreted; 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 "usage: up [--level <n>| -n <levels>][--help][--version]\n\nReport bugs to: \nup home page: \n"

注意,与echo不同,必须显式地包含最后的换行符。此外,如果消息碰巧包含%字符,则必须将它们写成%%。如果你想包含bugreport和主页地址,它们可以很自然地添加:

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

其次,你可以使用format字符串让它在单独的行中打印每个额外的参数:

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

使用这个选项,添加bugreport和主页地址是相当明显的:

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

使用-e选项,则可以在字符串中打印带\n的新行字符。

例如:

echo -e "This will be the first line \nand this will be on the second line"
    You can write your
    text
        freely,
                   in a separate: 
                             ----file.

然后

echo "$(</pathto/your_multiline_file)"

受到本页上富有洞察力的回答的启发,我创造了一个混合的方法,我认为这是最简单和更灵活的方法。你怎么看?

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

__usage="
Usage: $(basename $0) [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
"

那么我就可以简单地把它用作

echo "$__usage"

或者更好的是,当解析参数时,我可以在一行中回显它:

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

或者你可以这样做:

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

Report bugs to: 
up home page: "