如何在 Bash 中将字符串拆分为多行?
如何将长字符串常量拆分为多行?
我意识到你可以这样做:
echo "continuation \
lines"
>continuation lines
但是,如果你有缩进代码,效果就不太好:
echo "continuation \
lines"
>continuation lines
How can i split my long string constant over multiple lines?
I realize that you can do this:
echo "continuation \
lines"
>continuation lines
However, if you have indented code, it doesn't work out so well:
echo "continuation \
lines"
>continuation lines
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
这就是您可能想要的
如果这会创建两个 echo 参数,而您只需要一个,那么让我们看看字符串连接。在 bash 中,将两个字符串并排放置在一起:
因此,不带缩进的续行是分解字符串的一种方法:
但是当使用缩进时:
您会得到两个参数,因为这不是更长的串联。
如果您想要一个跨行的字符串,同时缩进但没有获得所有这些空格,您可以尝试的一种方法是放弃续行并使用变量:
这将允许您以额外变量为代价获得干净的缩进代码。如果你将变量设置为本地变量,那应该不会太糟糕。
This is what you may want
If this creates two arguments to echo and you only want one, then let's look at string concatenation. In bash, placing two strings next to each other concatenate:
So a continuation line without an indent is one way to break up a string:
But when an indent is used:
You get two arguments because this is no longer a concatenation.
If you would like a single string which crosses lines, while indenting but not getting all those spaces, one approach you can try is to ditch the continuation line and use variables:
This will allow you to have cleanly indented code at the expense of additional variables. If you make the variables local it should not be too bad.
这里带有
<<-HERE
终止符的文档非常适合缩进的多行文本字符串。它将从此处文档中删除所有前导选项卡。 (不过,行终止符仍将保留。)(Stack Overflow 将制表符呈现为空格,因此您无法直接从此处复制/粘贴代码。我在
continuation
和lines< 之前确实使用了文字制表符/code> 在此答案的来源中。)
另请参阅 http://ss64.com/bash/syntax-here.html
如果您需要保留一些(但不是全部)前导空格,您可以使用类似的内容
或可能使用
tr 摆脱换行符:(
第二行前面有一个制表符和一个空格;制表符将由破折号运算符在定界符终止符之前删除,而空格将被保留。)
用于将长的复杂字符串包装在许多线条,我喜欢
printf
:如果您想要将重要的 shell 脚本片段嵌入另一种语言,而宿主语言的语法不允许您使用此处文档,例如在
中,它也能很好地工作。 Makefile
或Dockerfile
。对于重要的需求,也许还探索
%b
printf
的格式字符串说明符。 (不过,这只是 Bash。)Here documents with the
<<-HERE
terminator work well for indented multi-line text strings. It will remove any leading tabs from the here document. (Line terminators will still remain, though.)(Stack Overflow renders tabs as spaces, so you cannot copy/paste the code directly from here. I did use literal tabs before
continuation
andlines
in the source of this answer.)See also http://ss64.com/bash/syntax-here.html
If you need to preserve some, but not all, leading whitespace, you might use something like
or maybe use
tr
to get rid of newlines:(The second line has a tab and a space up front; the tab will be removed by the dash operator before the heredoc terminator, whereas the space will be preserved.)
For wrapping long complex strings over many lines, I like
printf
:It also works well in contexts where you want to embed nontrivial pieces of shell script in another language where the host language's syntax won't let you use a here document, such as in a
Makefile
orDockerfile
.For nontrivial needs, perhaps also explore the
%b
format string specifier forprintf
. (This is Bash only, though.)您可以使用 bash 数组
然后
有一个额外的空格,因为(bash手册之后):
因此设置
IFS=''
来消除多余的空间You can use bash arrays
then
there is an extra space, because (after bash manual):
So set
IFS=''
to get rid of extra space在某些情况下,利用 Bash 的串联能力可能是合适的。
示例:
从 Bash 手册页的 PARAMETERS 部分:
In certain scenarios utilizing Bash's concatenation ability might be appropriate.
Example:
From the PARAMETERS section of the Bash Man page:
您可以根据缩进中的需要简单地用换行符(不使用反斜杠)将其分隔开,如下所示,然后删除新行。
示例:
或者,如果它是变量定义,则换行符会自动转换为空格。因此,仅在适用时删除额外的空格。
You could simply separate it with newlines (without using backslash) as required within the indentation as follows and just strip of new lines.
Example:
Or if it is a variable definition newlines gets automatically converted to spaces. So, strip of extra spaces only if applicable.
这并不完全是用户所要求的,但创建跨多行的长字符串的另一种方法是逐步构建它,如下所示:
显然,在这种情况下,一次构建它会更简单,但这种风格在处理较长的字符串时可以非常轻量级且易于理解。
This isn't exactly what the user asked, but another way to create a long string that spans multiple lines is by incrementally building it up, like so:
Obviously in this case it would have been simpler to build it one go, but this style can be very lightweight and understandable when dealing with longer strings.
行延续也可以通过巧妙地使用语法来实现。
对于
echo
的情况:对于 vars 的情况:
对于 vars 的另一种方法:
瞧!
Line continuations also can be achieved through clever use of syntax.
In the case of
echo
:In the case of vars:
Another approach in the case of vars:
Voila!
我遇到过一种情况,我必须发送一条长消息作为命令参数的一部分,并且必须遵守行长度限制。命令看起来像这样:
我解决这个问题的方法是将消息作为此处文档移出(就像 @tripleee 建议的那样)。但是这里的文档变成了标准输入,所以需要读回它,我采用了以下方法:
这样做的优点是
$message
可以完全用作字符串常量,没有额外的空格或换行符。请注意,上面的实际消息行均以
tab
字符为前缀,该字符被此处文档本身删除(因为使用<<-
)。末尾仍然有换行符,然后用带有空格的tr
替换。另请注意,如果您不删除换行符,它们将在
"$message"
展开时按原样显示。在某些情况下,您可以通过删除$message
周围的双引号来解决此问题,但该消息将不再是单个参数。I came across a situation in which I had to send a long message as part of a command argument and had to adhere to the line length limitation. The commands looks something like this:
The way I solved this is to move the message out as a here document (like @tripleee suggested). But a here document becomes a stdin, so it needs to be read back in, I went with the below approach:
This has the advantage that
$message
can be used exactly as the string constant with no extra whitespace or line breaks.Note that the actual message lines above are prefixed with a
tab
character each, which is stripped by here document itself (because of the use of<<-
). There are still line breaks at the end, which are then replaced bytr
with spaces.Note also that if you don't remove newlines, they will appear as is when
"$message"
is expanded. In some cases, you may be able to workaround by removing the double-quotes around$message
, but the message will no longer be a single argument.根据您将接受的风险类型以及您对数据的了解和信任程度,您可以使用简单的变量插值。
Depending on what sort of risks you will accept and how well you know and trust the data, you can use simplistic variable interpolation.
按照 @tripleee 的
printf
示例 (+1):我们在句子之间添加了明确的空格,例如“
' Yes
...”。另外,如果我们可以不使用变量:对永无止境的歌曲的致谢
Following @tripleee 's
printf
example (+1):And we have included explicit spaces between the sentences, e.g. "
' Yes
...". Also, if we can do without the variable:Acknowledgement for the song that never ends
尝试使用单引号并连接字符串:
注意:连接包含空格。
Try with single quotes and concatenating the strings:
Note: the concatenation includes a whitespace.
这可能并不能真正回答您的问题,但您可能会发现它很有用。
第一个命令创建由第二个命令显示的脚本。
第三个命令使该脚本可执行。
第四个命令提供了使用示例。
请注意,如果您确实想使用此脚本,则将可执行脚本移至
~/bin
中更有意义,这样它将位于您的路径中。检查 python 参考以获取有关如何
textwrap.dedent
< 的详细信息/a> 有效。如果
$'...'
或"$(...)"
的用法令您感到困惑,请询问另一个问题(每个构造一个)(如果还没有)一起来。最好提供您找到/提出的问题的链接,以便其他人可以获得链接的参考。This probably doesn't really answer your question but you might find it useful anyway.
The first command creates the script that's displayed by the second command.
The third command makes that script executable.
The fourth command provides a usage example.
Note that if you really want to use this script, it makes more sense to move the executable script into
~/bin
so that it will be in your path.Check the python reference for details on how
textwrap.dedent
works.If the usage of
$'...'
or"$(...)"
is confusing to you, ask another question (one per construct) if there's not already one up. It might be nice to provide a link to the question you find/ask so that other people will have a linked reference.