I am trying to pass arguments to a function within an if statement and then evaluating what the function returns (in bash). The result I am getting is incorrect. How do you properly do this?
#!/bin/bash
foo() {
if [ $1 = "zero" ]; then
echo "0"
else
echo "1"
fi
}
arg="zero"
if foo $arg -eq 0 ; then
echo "entered 0"
else
echo "entered something else"
fi
arg="blah"
if foo $arg -eq 0 ; then
echo "entered 0"
else
echo "entered something else"
fi
Not the desired results:
cknight@macbook:~/work$ ./test.sh
0
entered 0
1
entered 0
You can either
returna result from your function or use$()to capture its output: