I am trying to understand how to work with functions (that receive a argument) in bash. I did not get my code to work, the following code exemplifies my difficulties:
#!/bin/bash
fruit_code () {
if [[ "$1" == "apple" ]]; then
code=1
else
code=0
fi
return $code
}
for var in apple orange banana apple; do
code=fruit_code $var
echo $code
done
The shell (bash) complains saying:
apple: command not found
orange: command not found
banana: command not found
apple: command not found
So it seems the passing of parameters is not working propperly. I can not see where I am going wrong. Any help is very much appreciated. Why does it not work? What changes shoyld I do to make it work? Wish to thank you all in advance.
Kind regards Miguel