When I execute this
regex='^[-a-z0-9]+$'
string='abcd1--'
if [[ $string =~ $regex ] -a ![ grep - "--" ]]
then 
    echo "valid"
else
    echo "not valid"
fi
I get
~$ sh t.sh 
t.sh: line 3: syntax error in conditional expression
t.sh: line 3: syntax error near `]'
t.sh: line 3: `if [[ $string =~ $regex ] -a [ grep - "--" ]]'
~$ 
It is suppose to return not valid.
Can someone figure out what's wrong?
You're mixing [ and [[ syntax in a strange way.
Try:
if [[ ( $string =~ $regex ) && !( $string =~ "--" ) ]]
and check bash's man page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With