Jagdeep Jagdeep - 4 years ago 118
Bash Question

How to use 'AND' condition in 'test' command in bash shell

I am using below command to test the 'AND' condition

[[ "$string1" =~ "not" -a ! "$string1" =~ "$chas" ]] && echo "Hello"


but it is throwing syntex error

-bash: syntax error in conditional expression
-bash: syntax error near `-a'


I am not getting what is the syntax error here.
Individual conditions are working but when using and condition(-a) it is throwing error.

Answer Source

Test command [[ is different from [. In [[ -a file True if file exists.. Try && for AND bool operator... But the syntax is a bit different. =~ means regex pattern matching and the " not needed around env vars and regex expression.

So this works for me:

[[  $string1 =~ not && ! $string1 =~ $chas ]] && echo "Hello" 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download