Why is an integer expression expected?

Why do I have the expected integer expression error with this:

at=`echo $1 | grep -q "@"`
if [ $at -ne 0 ]; then
    echo "blabla"
else
    echo "bloblo"
fi

$atand the test works fine outside of the script

+3
source share
2 answers

When testing the result, grep -qyou want to test $?not the grep output, which will be empty

at=$(echo "$1" | grep -q "@")
if [ $? -ne 0 ]; then ...

or simply

if echo "$1" | grep -q "@"; then ...

or, more bash -ly

if grep -q "@" <<< "$1"; then ...

or without calling grep:

if [[ "$1" == *@* ]]; then ...

or

case "$1" in
  *@*) echo "match" ;;
  *) echo "no match" ;;
esac
+4
source

-nedesigned to compare integers. Use !=to compare strings.

+3
source

All Articles