1

When I run this script:

#!/bin/bash

if [[ "abcd" =~ ^.*$ ]]; then
    echo "something"
fi

I get:

./tmp2.sh: line 3: conditional binary operator expected
./tmp2.sh: line 3: syntax error near `=~'
./tmp2.sh: line 3: `if [[ "abcd" =~ ^.*$ ]]; then'

I've tried all suggestions I've found, but still the same:/ Help me, please!

3
  • I've failed to reproduce this behaviour. Commented Jul 12, 2014 at 14:37
  • 4
    How are you running the script? Are you sure the script is being run by bash, not some other shell or bash in POSIX mode? Commented Jul 12, 2014 at 15:16
  • 2
    Which version of bash are you running? Commented Jul 12, 2014 at 16:19

4 Answers 4

6

Given that you're seeing a bash-specific error message, we can rule out that something other than bash is running the script (if it were a POSIX-features-only shell, such as sh on some systems, you'd instead see an error message relating to [[).

The most likely explanation:

  • Your bash version is < 3.0, and therefore doesn't support the =~ operator.
    • To verify, run echo $BASH_VERSION in your script.

The specific error you're seeing is bash's way of saying: "I'm seeing a string I don't recognize as an operator."

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

2 Comments

You got it right. I should have checked earlier versions too.
Sounds likely, but concerning how horribly outdated OP's system is. Bash 3 turns 10 in a couple weeks.
3

Run your script with bash script.sh not sh script.sh. It probably would also work with ./script.sh if script.sh is already executable since you're using the header #!/bin/bash.

Comments

1

this should work

re='^.*$'
if [[ "abcd" =~ $re ]]; then
  echo "something"
fi

3 Comments

It would be helpful to explain the syntax. Please expand on your answer.
This wouldn't help. The =~ syntax works in all versions of Bash starting 3.0.
While storing your regex in an auxiliary variable does help in some situations (with regexes that contain \ -prefixed sequences such as \< or \s), this is NOT the problem here - yours an the OP's code are equivalent.
0

This is almost identical to the error you get in all versions of bash that support regex if you put ~= rather than =~.

4 Comments

How is this answering the question?
Because I came here from google trying to investigate why I was getting "conditional binary operator expected" and it wasn't until I copied the example from here did I spot the obvious error. It looks almost identical to the error listed above and I have spent quite a while searching for this resolution. If you are on this page with that error and you haven't checked the =~ it is probably a far more common reason it's not working.
May be, but this doesn't answer actual question and shouldn't be here, SO is not a forum.
When you google "stack overflow" "syntax error near" "~=", this page is top of the list. It is not immediately obvious that google has swapped the =~ for you. This post answers that related question.

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.