Search code examples
regexbashshellformatdash-shell

Regex compatibility across multiple shells


I'm checking correct date format in my bash script with following code:

if [[ $variable == [0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9] ]]

The format should be: YYYY-MM-DD

This works well in bash, however I have problems when trying to run it in dash or sh. Could you help me rewriting this so it's compatible with dash and sh? Or alternatively find a different solution that can be used on all shells?

Thanks in advance !


Solution

  • case should work in dash, too:

    case 0015-18-32 in 
        [0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9] ) echo yes ;;
    esac