Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Let's say I have this simple code:

echo "Are there any arguments?"
if [ $# -eq 0 ]; then
    echo "false"
else
    echo "true"
fi

As you can see it would be better to just have opportunity to directly print condition result, but I don't know how to do it.

It would be something like:

echo "$([ $# -eq 0 ])"

But it doesn't work that way. Can we do this without if?

share|improve this question

You can use the list control operators && and || instead:

[[ $# -eq 0 ]] && { echo false; } || { echo true; }

The { } group a list of commands, you don't need them for just a single command, but they often make such constructions more readable.

share|improve this answer

You can use $? that keeps the exit code of the last executed command:

echo "Are there any arguments?"
[ $# -eq 0 ]
echo $?
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.