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.
精彩评论