0

In bash, why doesn't this work:

$ echo $((1 -gt 2 ? 3 : 4))
bash: 1 -gt 2 ? 3 : 4: syntax error in expression (error token is "2 ? 3 : 4")

Neither does this:

$ echo $(((1 -gt 2) ? 3 : 4))
bash: (1 -gt 2) ? 3 : 4: missing `)' (error token is "2) ? 3 : 4")

1 Answer 1

2

Use:

if [ 1 -gt 2 ]; then
  echo 3
else 
  echo 4
fi

Or:

echo $((2 > 1 ? 1 : 0))

The -gt family is used by the test command, while the operators allowed in $(()) are described here and here. You can't mix and match.

Note from the standard that "only signed long integer arithmetic is required." You need to use bc.

Sign up to request clarification or add additional context in comments.

1 Comment

Because bash doesn't support floating point. Use the bc command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.