-1

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

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Miguel Garcia
  • 97
  • 1
  • 8
  • See: [How to store command results in a shell variable?](https://stackoverflow.com/q/19951369/3776858) – Cyrus Jun 07 '22 at 21:16

2 Answers2

2

The first problem is that if you want to execute a command and capture its output to a variable, you need to use command substitution:

code=$(fruit_code "$var")

The second problem is that your function doesn't write anything to standard output; it returns an exit status. That is automatically assigned to $? after you call the function. Use this instead:

fruit_code "$var"
echo $?

Finally, the convention in shell is for a 0 exit status to indicate success and a non-zero value failure. In this case, your function "succeeds" if the argument is not apple.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Hello, thanks that worked! However I tried with my functión returning strings and it did not work. Say for example that instead of my function returning 0/1 y want to return no/yes how would I proceed? – Miguel Garcia Jun 07 '22 at 21:44
1

You have a syntax error in this line:

code=fruit_code $var

A syntax like this:

foo=bar quux

means that you want to run command quux in an environment where variable foo has value bar.

In your case, quux is $var, so it will take the value of $var and try to run it as a command. That's why you are getting errors saying apple, orange, etc. are not commands.

I think what you actually want is to run fruit_code $var and store the output in variable code, right?

In that case you can use this:

code=$(fruit_code $var)

or this:

code=`fruit_code $var`

I prefer the former because it looks clearer to me and allows easy nesting.

Update: Returning strings in functions

Also, as noted by chepner on his answer and as you also noticed in a comment below, you are trying to use return for returning that string value as the result of the function, but return can only be used for returning numeric values as the result of the function (success / error / etc.) so you try to work it around by using numeric values that represent the strings you want to return.

That's overcomplex.

If you want to return a string, use echo instead and you can use $() or `` as explained above to capture the string returned by your function.

So, putting all this together, your code would look like this:

#!/bin/bash

fruit_code () {
        echo $1
}


for var in apple orange banana apple; do
        code=$(fruit_code $var)
        echo $code
done
chuso
  • 91
  • 5
  • Hello, thanks that worked! However I tried with my functión returning strings and it did not work. Say for example that instead of my function returning 0/1 y want to return no/yes how would I proceed? – Miguel Garcia Jun 07 '22 at 21:53
  • 1
    You have to use `echo $code` instead of `return $code` for returning strings. I just updated my answer with more details. – chuso Jun 08 '22 at 08:30