如何在Bash中分割多行字符串?
如何将长字符串常量拆分为多行 我意识到你可以做到这一点:如何在Bash中分割多行字符串?,bash,line,indentation,Bash,Line,Indentation,如何将长字符串常量拆分为多行 我意识到你可以做到这一点: echo "continuation \ lines" >continuation lines 但是,如果您有缩进的代码,它的效果就不太好: echo "continuation \ lines" >continuation lines echo "continuation \ lines" >continuation lines
echo "continuation \
lines"
>continuation lines
但是,如果您有缩进的代码,它的效果就不太好:
echo "continuation \
lines"
>continuation lines
echo "continuation \
lines"
>continuation lines
这就是你想要的
$ echo "continuation"\
> "lines"
continuation lines
如果这为echo创建了两个参数,而您只需要一个,那么让我们看看字符串连接。在bash中,将两个字符串相邻放置以连接:
$ echo "continuation""lines"
continuationlines
因此,不带缩进的连续行是分解字符串的一种方法:
$ echo "continuation"\
> "lines"
continuationlines
但当使用缩进时:
$ echo "continuation"\
> "lines"
continuation lines
您将得到两个参数,因为这不再是串联
如果您想要一个跨行的字符串,同时缩进但不获取所有空格,可以尝试的一种方法是舍弃连续行并使用变量:
$ a="continuation"
$ b="lines"
$ echo $a$b
continuationlines
这将允许您在牺牲额外变量的情况下获得清晰缩进的代码。如果您将变量设置为局部变量,这应该不会太糟糕。这里是带有
的文档,这可能不会真正回答您的问题,但您可能会发现它很有用
第一个命令创建由第二个命令显示的脚本
第三个命令使该脚本可执行
第四个命令提供了一个使用示例
john@malkovich:~/tmp/so$ echo $'#!/usr/bin/env python\nimport textwrap, sys\n\ndef bash_dedent(text):\n """Dedent all but the first line in the passed `text`."""\n try:\n first, rest = text.split("\\n", 1)\n return "\\n".join([first, textwrap.dedent(rest)])\n except ValueError:\n return text # single-line string\n\nprint bash_dedent(sys.argv[1])' > bash_dedent
john@malkovich:~/tmp/so$ cat bash_dedent
#!/usr/bin/env python
import textwrap, sys
def bash_dedent(text):
"""Dedent all but the first line in the passed `text`."""
try:
first, rest = text.split("\n", 1)
return "\n".join([first, textwrap.dedent(rest)])
except ValueError:
return text # single-line string
print bash_dedent(sys.argv[1])
john@malkovich:~/tmp/so$ chmod a+x bash_dedent
john@malkovich:~/tmp/so$ echo "$(./bash_dedent "first line
> second line
> third line")"
first line
second line
third line
请注意,如果您真的想使用此脚本,那么将可执行脚本移动到~/bin
中,使其位于您的路径中更为合理
有关如何工作的详细信息,请查看python参考
如果$'…'
或“$(…)”
的用法让您感到困惑,请询问另一个问题(每个构造一个),如果还没有。提供一个指向您发现/询问的问题的链接可能会更好,这样其他人就会有一个链接引用。我遇到了一种情况,在这种情况下,我必须发送一条长消息作为命令参数的一部分,并且必须遵守行长度限制。命令如下所示:
somecommand --message="I am a long message" args
我解决这个问题的方法是将消息作为here文档移出(如@tripleee所建议的)。但此处的文档变成了标准文本,因此需要重新读取,我采用以下方法:
message=$(
tr "\n" " " <<- END
This is a
long message
END
)
somecommand --message="$message" args
消息=$(
tr“\n”“”
但是,如果您有缩进的代码,它的效果就不太好:
echo "continuation \
lines"
>continuation lines
echo "continuation \
lines"
>continuation lines
尝试使用单引号并连接字符串:
echo 'continuation' \
'lines'
>continuation lines
注意:连接包括空格。您可以使用
然后
还有一个额外的空间,因为(在bash手册之后):
如果单词是双引号,${name[*]}
将扩展为一个带有
由数组的第一个字符分隔的每个数组成员的值
IFS变量
因此,设置IFS='
以消除多余的空间
$ IFS=''
$ echo "${str_array[*]}"
continuationlines
您可以根据需要在缩进中使用换行符(不使用反斜杠)将其分隔开来,如下所示,并只保留一条新行
例如:
echo "continuation
of
lines" | tr '\n' ' '
或者,如果它是一个变量定义,新行会自动转换为空格。因此,只有在适用的情况下,才能去掉额外的空格
x="continuation
of multiple
lines"
y="red|blue|
green|yellow"
echo $x # This will do as the converted space actually is meaningful
echo $y | tr -d ' ' # Stripping of space may be preferable in this case
这并不完全是用户所要求的,但是创建跨多行的长字符串的另一种方法是通过增量构建它,如下所示:
$ greeting="Hello"
$ greeting="$greeting, World"
$ echo $greeting
Hello, World
显然,在这种情况下,一次过构建它会更简单,但在处理较长字符串时,这种样式非常轻量级且易于理解。根据您将接受何种风险以及您对数据的了解和信任程度,您可以使用简单的变量插值
$: x="
this
is
variably indented
stuff
"
$: echo "$x" # preserves the newlines and spacing
this
is
variably indented
stuff
$: echo $x # no quotes, stacks it "neatly" with minimal spacing
this is variably indented stuff
在某些情况下,利用Bash的连接功能可能是合适的
例子:
从Bash手册页的参数部分:
名称=[值]
…在赋值语句将值赋给外壳变量或数组索引的上下文中,+=运算符可用于追加或添加到变量的上一个值。当+=应用于已设置integer属性的变量时,该值将作为算术表达式求值并添加到变量的当前值,该值也会被计算。当使用复合赋值将+=应用于数组变量时(请参见下面的数组),变量的值不会取消设置(就像使用=)时一样),新值将从大于数组最大索引的一个开始追加到数组中(对于索引数组)或作为附加键值对添加到关联数组中。应用于字符串值变量时,值将展开并附加到变量的值。
行连续体也可以通过巧妙地使用语法来实现
如果出现回声
:
# echo '-n' flag prevents trailing <CR>
echo -n "This is my one-line statement" ;
echo -n " that I would like to make."
This is my one-line statement that I would like to make.
VAR的另一种方法是:
outp="This is my one-line statement" ;
outp+=" that I would like to make." ;
echo -n "${outp}"
This is my one-line statement that I would like to make.
outp="This is my one-line statement" ;
outp="${outp} that I would like to make." ;
echo -n "${outp}"
This is my one-line statement that I would like to make.
瞧!谢谢您的帮助,虽然这确实删除了空格,但它们现在是单独的参数(Bash将第二行上的空格解释为参数分隔符),并且由于echo命令,现在只能正确打印。哦,您想要一个(Bash)字符串到跨行!我现在明白了。解决方案只有一个变量:s=“字符串编号1”s+=“字符串编号2”s+=“字符串编号3”echo“$s”
虽然这可能是出于好意,甚至可能是有用的,但OP向他征求关于基本bash语法的建议,你给了他一个python函数定义,它使用OO范例、异常流控制和导入。此外,你调用了一个可执行文件作为字符串插值的一部分,这是一个提出此类问题的人所需要的肯定还没有在bash中看到。Google的bash Shell风格指南,用于“如果您必须编写长度超过80个字符的字符串”。请参阅。--这是新文档中的直接链接答案清楚地表明bash语言是多么有趣:-)它可以处理回显和字符串参数,但不能处理其他内容,比如变量赋值。虽然问题不是关于变量,但使用echo只是一个例子。而不是echo
如果您有x=$ greeting="Hello"
$ greeting="$greeting, World"
$ echo $greeting
Hello, World
$: x="
this
is
variably indented
stuff
"
$: echo "$x" # preserves the newlines and spacing
this
is
variably indented
stuff
$: echo $x # no quotes, stacks it "neatly" with minimal spacing
this is variably indented stuff
temp='this string is very long '
temp+='so I will separate it onto multiple lines'
echo $temp
this string is very long so I will separate it onto multiple lines
# echo '-n' flag prevents trailing <CR>
echo -n "This is my one-line statement" ;
echo -n " that I would like to make."
This is my one-line statement that I would like to make.
outp="This is my one-line statement" ;
outp+=" that I would like to make." ;
echo -n "${outp}"
This is my one-line statement that I would like to make.
outp="This is my one-line statement" ;
outp="${outp} that I would like to make." ;
echo -n "${outp}"
This is my one-line statement that I would like to make.