如何将我的长字符串常量分成多行?
我知道你可以这样做:
echo "continuation \
lines"
>continuation lines
但是,如果你有缩进的代码,它就不太好用了:
echo "continuation \
lines"
>continuation lines
如何将我的长字符串常量分成多行?
我知道你可以这样做:
echo "continuation \
lines"
>continuation lines
但是,如果你有缩进的代码,它就不太好用了:
echo "continuation \
lines"
>continuation lines
However, if you have indented code, it doesn't work out so well:
echo "continuation \ lines" >continuation lines
尝试使用单引号并连接字符串:
echo 'continuation' \
'lines'
>continuation lines
x=
而不是 echo
,你会得到错误信息:lines: command not found
。 - Mr. Lance E Sloan这可能并不完全回答了你的问题,但你可能会发现它仍然很有用。
第一个命令创建了由第二个命令显示的脚本。
第三个命令使该脚本可执行。
第四个命令提供了一个使用示例。
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
中,这样它就在您的路径中了。textwrap.dedent
的详细信息,请参阅Python参考文档。$'...'
或"$(...)"
的用法令您感到困惑,请提出另一个问题(每个结构一个问题),如果还没有问题,请提供一个链接,以便其他人可以进行参考。