Bash: 将长字符串参数拆分为多行?
Bash: split long string argument to multiple lines?
给定一个带有单个长字符串参数的命令,例如:
mycommand -arg1 "very long string which does not fit on the screen"
是否有可能以类似于 \
.
拆分单独参数的方式以某种方式拆分它
我试过:
mycommand -arg1 "very \
long \
string \
which ..."
但这不起作用。
mycommand
是外部命令,因此无法修改为采用单个参数。
你试过没有引号吗?
$ foo() { echo -e "1-\n2-\n3-"; }
$ foo "1 \
2 \
3"
1-1 2 3
2-
3-
$ foo 1 \
2 \
3
1-1
2-2
3-3
当您将它封装在双引号中时,它会尊重您的反斜杠并忽略后面的字符,但是由于您将整个内容用引号引起来,它会认为引号中的整个文本块应该被视为单个参数。
您可以将字符串分配给这样的变量:
long_arg="my very long string\
which does not fit\
on the screen"
那么只要使用变量:
mycommand "$long_arg"
在双引号内,删除了以反斜杠开头的换行符。请注意,字符串中所有其他白色 space 都很重要,即它将出现在变量中。
给定一个带有单个长字符串参数的命令,例如:
mycommand -arg1 "very long string which does not fit on the screen"
是否有可能以类似于 \
.
我试过:
mycommand -arg1 "very \
long \
string \
which ..."
但这不起作用。
mycommand
是外部命令,因此无法修改为采用单个参数。
你试过没有引号吗?
$ foo() { echo -e "1-\n2-\n3-"; }
$ foo "1 \
2 \
3"
1-1 2 3
2-
3-
$ foo 1 \
2 \
3
1-1
2-2
3-3
当您将它封装在双引号中时,它会尊重您的反斜杠并忽略后面的字符,但是由于您将整个内容用引号引起来,它会认为引号中的整个文本块应该被视为单个参数。
您可以将字符串分配给这样的变量:
long_arg="my very long string\
which does not fit\
on the screen"
那么只要使用变量:
mycommand "$long_arg"
在双引号内,删除了以反斜杠开头的换行符。请注意,字符串中所有其他白色 space 都很重要,即它将出现在变量中。