user3290805
user3290805

Reputation: 475

bash script condition check statement throwing error

Hi I am getting error in this line below

  BOOTPROTO="dhcp"
  TEST=$([[ "$BOOTPROTO" =~ (static|none|bootp|dhcp)$ ]] ; echo $?)

error I am getting is below

 -bash: command substitution: line 1: unexpected argument `(' to conditional   binary operator
 -bash: command substitution: line 1: syntax error near `(s'
 -bash: command substitution: line 1: `[[ "$BOOTPROTO" =~ (static|none|bootp|dhcp)$ ]] ; echo $?'

I tried to run it manually but could not solve it

Upvotes: 2

Views: 69

Answers (2)

user1934428
user1934428

Reputation: 22217

Why not simply

[[ "$BOOTPROTO" =~ (static|none|bootp|dhcp)$ ]]
TEST=$?

?

Upvotes: 0

jkdba
jkdba

Reputation: 2509

Wrap your regex in quotes:

  BOOTPROTO="dhcp"
  TEST=$([[ "$BOOTPROTO" =~ "(static|none|bootp|dhcp)$" ]] ; echo $?)
  echo "$TEST"

Or if you are using Bash >= 3.2, then escape the parentheses (as Eric mentioned in the comments.)

TEST=$([[ "$BOOTPROTO" =~ \(static|none|bootp|dhcp\)$ ]] ; echo $?)

Output:

1

Upvotes: 3

Related Questions