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 represent clubs which have multiple members (each member can also join
I have multiple thumbnails and want the main image to represent a large version
NOTE : In hindsight, I realize this may seem like a stupid question, but
I have a string with multiple sequences of consecutive characters like: aaabbcccdddd I want
I want to represent a DAG as JSON text and wondering if anyone has
We have objects that we want to represent in stacks (think of stacking items
In SQL Server 2008, I want to represent an integer as a 3-character string
I want to parse a string that represent a DateTime in UTC format. My
I have a database and I've created a DBML Linq-to-SQL file to represent this
I need to create several combo boxes to represent enumeration values. I want the

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.