1

This is my code:

if [ $value == "^[[:digit:]]*$" ]
then
    echo "number"
else
    echo "other"
fi

It seems that the program never echoes "number" even if I set value=1.

1 Answer 1

7

test doesn't do regexs.

[[ $value =~ ^[0-9]+$ ]]
Sign up to request clarification or add additional context in comments.

3 Comments

+1 Beat me to it. Also important is that the OP needs to not have quotes around the regex. Plus the question was missing a then ...
could I ask that what ~ refers to?
Short answer: =~ is a distinct operator, just like == and !=

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.