Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7548179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:38:01+00:00 2026-05-30T09:38:01+00:00

This is my program try.sh: in=$* type=(even odd) echo -e $in is a ${type[is_odd

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:38:02+00:00Added an answer on May 30, 2026 at 9:38 am

    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”.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program stores pairs in a map, counting the number of times a word
This program is meant to generate a dynamic array, however it gives an access
I'm going through the tekpub rack tutorial but when I try to run even
EDIT-4 I've gotten my sitecustomize.py to execute, but it tosses up an error. Here's
This program is supposed to judge the height of multiple buildings by the type
When I try and compile this program, I get errors (included below the code)
This program I use has it's own variables to set when you run it,
This program reads emails (really just a .txt file structured like an email) and
This program I'm doing is about a social network, which means there are users
For this program #include <iostream> using std::cout; struct C { C() { cout <<

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.