Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional binary operator expected: syntax error near `$1' [duplicate]

Tags:

bash

shell

I am trying to run simple code block. It gives error on fourth line as "syntax error near `$1'"

=~ means Matches Regular Expression

How should I use '$1' variable with this operator?

Here is my code:

if [[ $1 -gt 3 ]] && [[ $1 -lt 7 ]]
then
echo "$1 is between 3 and 7"
elif [[ $1 =~ "Jeff"]] || [[ $1 =~ "Roger" ]] || [[ $1 =~ "Brian" ]]
then
echo "$1 works in the Data Science Lab"
else
echo "You entered: $1, not what I was looking for.."
fi
like image 924
Bce Avatar asked Feb 20 '26 00:02

Bce


1 Answers

Very funny indeed. You've typed the first condition in that line as [[ $1 =~ "Jeff"]], so without a space between "Jeff" and ]] bash interprets them as a single string, which is obviously not your pattern, and the whole parse fails and line structure crashes. If you add that space:

if [[ $1 =~ "Jeff" ]] || [[ $1 =~ "Roger" ]] || [[ $1 =~ "Brian" ]]

then it works... seemingly...

like image 160
bipll Avatar answered Feb 22 '26 20:02

bipll



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!