开发者

Can a function be used in $(( func arg ? str1 : str2 ))?

开发者 https://www.devze.com 2023-03-11 03:23 出处:网络
I\'d like to use a statement like this: var=$(( func arg ? str1 : str2 )) but bash gives this syntax error message:

I'd like to use a statement like this:

var=$(( func arg ? str1 : str2 ))

but bash gives this syntax error message:

syntax error in expression (error token is "arg")

I've played with various forms but 开发者_运维知识库I can't figure out how to make it accept a function with argument. Any ideas?


echo $(( $(seq 1) + 1 ))
2

You need to use the same syntax as bash expects elsewhere. As far as the conditional ? iftrue : iffalse syntax, I don't think you can do that in bash. Instead, you can do something like:

echo $(( 1 + $(true && echo 1 || echo 0) ))
2


I think the correct answer is that there is no way to use the statement I asked about. The problem is that this conditional operator can only evaluate to an integer and not a string as I wanted to do.

jm666 answered the question in a comment to Steve's answer so I gave him an up vote.

0

精彩评论

暂无评论...
验证码 换一张
取 消