Search code examples
bash

Can you use a function call as a condition in an if statement?


here's what i'm trying to achive:

function f1() {
  return 0
}

function f2() {
  return 0
}

if [[ f1 && f2 ]]; then
  echo "success"
else
  echo "fail"
fi

Solution

  • You don't use [[ (or [) when running a command and checking the result code.

    if f1 && f2 ; then
      echo "success"
    else
      echo "fail"
    fi