I'm trying to write a simple script to check if the current hour is before or after 12:00 AM.

var1=`date +"%T"` # error "not an integer"
if [ $var1 -lt $var2 ]; then # always false
    echo "Good morning!"
    echo "Good afternoon!"

The error says that var1 must be an integer number, and the condition is always false. I would like some help please. Thank you very much.

  • Suggestion: var1=$(date + "%T") Dec 17 '19 at 9:56

The test you are using can only compare integer numbers. date +%T produces strings containing :, so your test fails.

You could use date +%H and set var2=12.

Or you could use date +%H%M%S and set var2=120000.

That being said, I'd suggest you use $(date ...) instead of the backticks. It's much easier to read and allows you to wrap multiple commands into one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.