Skip to content
Advertisement

check if argument is a valid date in bash shell

I am writing a bash shell script in Linux, this program will accept a date (mm-dd-yyyy) as a parameter. I am wondering if there is a simply way to check if the date is valid? is there an operator and I can just use test to check?

Advertisement

Answer

You can check with date -d "datestring"

So date -d "12/31/2012" is valid, but using hyphens, e.g. date -d "12-31-2012", is not valid for date.

You can also use words: date -d 'yesterday' or date -d '1 week ago' are both valid.

Advertisement