[英]Can I use the $( ... ) syntax with multiline commands?
I often use backticks in with long commands that I break up with backslash-newlines for readability.我经常在长命令中使用反引号,为了可读性,我用反斜杠换行符分开。 When I replace the backticks with the $( ... )
syntax, I get errors.当我用$( ... )
语法替换反引号时,出现错误。
For instance, I set a variable using curl
and jq
inside backquotes (reading from my Trello daily to-do list and returning card.id and card.name for everything but a specific card):例如,我在反引号中使用curl
和jq
设置了一个变量(从我的 Trello 每日待办事项列表中读取并为除特定卡片之外的所有内容返回 card.id 和 card.name):
AllCardsInDTL=\
`\
curl -s "https://api.trello.com/1/lists/$DailyTasksListID/cards\
?$WhoMe\
&fields=name,id,pos\
"\
| jq '\
.[] | \
if .name | test ("Tasks here are copied.*") \
then empty \
else .id, .name \
end \
'\
` # End of AllCardsInDTL=
That has no problems and the shell variable consists of alternating lines of ID and Name values.这没有问题,shell 变量由 ID 和 Name 值的交替行组成。
But if I replace the backquoteswith $( ... )
, I get an error from jq
:但是如果我用$( ... )
替换反引号,我会从jq
得到一个错误:
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
\
jq: 1 compile error
(23) Failed writing body
It's not clear to me from the error, whether the )
is not actually terminating the command started by $(
or whether inside the $( ... )
the backslash is not being interpreted as I expect.从错误中我不清楚)
是否实际上终止了由$(
启动的命令,或者在$( ... )
中是否没有按照我的预期解释反斜杠。
I would like to be able to use the $( ... )
syntax because there are times when I want to nest one subshell inside another and that's very difficult to do with backquotes.我希望能够使用$( ... )
语法,因为有时我想将一个子shell嵌套在另一个子shell中,而使用反引号很难做到这一点。
You can certainly use \\
to escape end-of-lines inside a $(...)
expression.您当然可以使用\\
来转义$(...)
表达式中的行尾。 Also note that you don't need those \\
's in the jq
expression inside the single quotes.还要注意的是,你不需要这些\\
“S IN的jq
在单引号内的表达。
I would probably write it something like this:我可能会这样写:
AllCardsInDTL=$(
curl -s "https://api.trello.com/1/lists/$DailyTasksListID/cards\
?$WhoMe\
&fields=name,id,pos" |
jq '.[] |
if .name | test ("Tasks here are copied.*")
then empty
else .id, .name
end
'
)
I can't test this, since I don't have a useful value for $DailyTasksListID
nor am I equipped to authenticate to trello at the moment.我无法对此进行测试,因为我没有$DailyTasksListID
的有用值,目前我也没有能力对$DailyTasksListID
进行身份验证。
Yes.是的。 Demo:演示:
echo $(echo foo
echo bar
echo baz
)
Output:输出:
foo bar baz
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.