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 1102933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:15:47+00:00 2026-05-17T01:15:47+00:00

I want to represent multiple conditions like this: if [ ( $g -eq 1

  • 0

I want to represent multiple conditions like this:

if [ ( $g -eq 1 -a "$c" = "123" ) -o ( $g -eq 2 -a "$c" = "456" ) ]   
then  
    echo abc;  
else  
    echo efg;   
fi  

but when I execute the script, it shows

syntax error at line 15: `[' unexpected,

where line 15 is the one showing if ….

What is wrong with this condition? I guess something is wrong with the ().

  • 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-17T01:15:47+00:00Added an answer on May 17, 2026 at 1:15 am

    Classic technique (escape metacharacters):

    if [ \( "$g" -eq 1 -a "$c" = "123" \) -o \( "$g" -eq 2 -a "$c" = "456" \) ]
    then echo abc
    else echo efg
    fi
    

    I’ve enclosed the references to $g in double quotes; that’s good practice, in general. Strictly, the parentheses aren’t needed because the precedence of -a and -o makes it correct even without them.

    Note that the -a and -o operators are part of the POSIX specification for test, aka [, mainly for backwards compatibility (since they were a part of test in 7th Edition UNIX, for example), but they are explicitly marked as ‘obsolescent’ by POSIX. Bash (see conditional expressions) seems to preempt the classic and POSIX meanings for -a and -o with its own alternative operators that take arguments.


    With some care, you can use the more modern [[ operator, but be aware that the versions in Bash and Korn Shell (for example) need not be identical.

    for g in 1 2 3
    do
        for c in 123 456 789
        do
            if [[ ( "$g" -eq 1 && "$c" = "123" ) || ( "$g" -eq 2 && "$c" = "456" ) ]]
            then echo "g = $g; c = $c; true"
            else echo "g = $g; c = $c; false"
            fi
        done
    done
    

    Example run, using Bash 3.2.57 on Mac OS X:

    g = 1; c = 123; true
    g = 1; c = 456; false
    g = 1; c = 789; false
    g = 2; c = 123; false
    g = 2; c = 456; true
    g = 2; c = 789; false
    g = 3; c = 123; false
    g = 3; c = 456; false
    g = 3; c = 789; false
    

    You don’t need to quote the variables in [[ as you do with [ because it is not a separate command in the same way that [ is.


    Isn’t it a classic question?

    I would have thought so. However, there is another alternative, namely:

    if [ "$g" -eq 1 -a "$c" = "123" ] || [ "$g" -eq 2 -a "$c" = "456" ]
    then echo abc
    else echo efg
    fi
    

    Indeed, if you read the ‘portable shell’ guidelines for the autoconf tool or related packages, this notation — using ‘||‘ and ‘&&‘ — is what they recommend. I suppose you could even go so far as:

    if [ "$g" -eq 1 ] && [ "$c" = "123" ]
    then echo abc
    elif [ "$g" -eq 2 ] && [ "$c" = "456" ]
    then echo abc
    else echo efg
    fi
    

    Where the actions are as trivial as echoing, this isn’t bad. When the action block to be repeated is multiple lines, the repetition is too painful and one of the earlier versions is preferable — or you need to wrap the actions into a function that is invoked in the different then blocks.

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

Sidebar

Related Questions

I want to assign the decimal variable "trans" to the double variable "this.Opacity". decimal
Let's say I want to represent \q (or any other particular backslash-escaped character). That
I'm rolling my own logger class, and want to represent the heirarchy of logs
I want a single number that represents the current date and time, like a
I need to create several combo boxes to represent enumeration values. I want the
What i want is to take an integer represented as a string, for example
Want to know what the stackoverflow community feels about the various free and non-free
Want my FireFox at work to be in sync with my FireFox at my
I want to loop over the contents of a text file and do a
I want to create a Java application bundle for Mac without using Mac. According

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.