Why double square brackets don't work in if statement with grep?

309 Views Asked by At

Can somebody explain why this bash script doesn't work when the if statement is in double square brackets, while without any brackets it does?

if [[ echo "word1word2" | grep -Eiq 'word1|word2' && echo "word3" | grep -Eiq 'word3' ]]
then
  echo "proceed"
else
  echo "don't proceed"
fi
1

There are 1 best solutions below

2
On

You don't need test operator at all:

if echo "word1word2" | grep -Eiq 'word1|word2' &&
   echo "word3" | grep -Eiq 'word3'
then
  echo "proceed"
else
  echo "don't proceed"
fi

It use boolean logic

If you want to use [[, then:

if [[ $(echo "word1word2" | grep -Ei 'word1|word2') && $(echo "word3" | grep -Ei 'word3') ]]
then
  echo "proceed"
else
  echo "don't proceed"
fi