0

This is my program "try.sh":

in=$*
type=(even odd)

echo -e $in " is a " ${type[is_odd $in]} " number."

is_odd()
{
    return `expr $1 % 2`
}

But if I execute "./try.sh" it gives me this error:

./try.sh: line 3: is_odd 2: syntax error in expression (error token is "2")

I want the return value of the function is_odd() to be passed as an index to the array named "type"

Please tell me how can I make it work. Thanks.

1 Answer 1

3

Rather than having is_odd return its result as its status-code, I think it's better to print its result:

is_odd()
{
    expr $1 % 2
}

Then you can use command-substitution (`...` or $(...)) to get the result:

echo -e $in " is an " ${type[$(is_odd $in)]} " number."

Though to be honest, in this specific case I'd probably just get rid of the function and use the arithmetic expression directly — and probably adjust the quoting a bit for readability:

echo -e "$in is an ${type[in % 2]} number."

(Note that double quotes "..." do not prevent parameter substitution ${...}. Only single-quotes '...' and backslashes \ do that. Also, hat-tip to jordanm for pointing out that array indices are automatically treated as arithmetic expressions, even without expr or ((...)) or whatnot.)

That said, if you really want to return 1 for "odd" and 0 for "even", then firstly, you should rename your function to is_even (since in Bash, 0 means "successful" or "true" and nonzero values mean "error" or "false"), and secondly, you can use a follow-on command to print its return value, and then use command-substitution. Either of these should work:

echo -e "$in is an ${type[$(is_even $in ; echo $?)]} number."

echo -e "$in is an ${type[$(is_even $in && echo 0 || echo 1)]} number."

(By the way, I've also changed a to an in all of the above examples: in English it's "an odd number", "an even number".)

Sign up to request clarification or add additional context in comments.

5 Comments

@ruakah inside of the [] array index, is already in numeric context, so $(()) is not needed.
@jordanm: Woah, I was completely unaware of that. That's good to know! I'll update my answer. Thank you very much!
@ruakh Thanks for the answer.. And actually I had "an" only in my code. I typed the question here quickly without thinking much so missed it.
@ruakh Using command substitution works fine. But is there any possible way to pass the return value of a function directly as an index of an array? Something like this ${type[is_odd $in]}? Or is it impossible?
@Vigneshwaran: I really think it's impossible. Shell functions are just commands, and their "return values" are just exit statuses indicating "successful"/"true" or "error"/"false". The usual way for a command to return real information is to print it out so that command-substitution can capture it. (In theory, I could imagine some sort of concept of "arithmetic function", declared in a different way, for use in arithmetic expressions, but there's currently no such thing.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.