I have a Bash function f, which takes a string value s as one of its arguments.
I need to abort execution if s is not an integer between [1000-9999].
I initially thought I would try to cast the string to an int and perform arithmetic range checks on the result, aborting on errors, but all other parameters are checked using RegEx so I might as well be consistent. I have to, actually.
Bash version: 4+
I could not come up with a better-looking pattern than this:
[[ "$s" =~ [[:\<:]][[:digit:]]{4}[[:\>:]] ]]
Can you help me improve the readability of the pattern-check construct above?
What I particularly dislike in the pattern above is:
- having to backslash escape the word boundary character
<as[[:\<:]] - the sheer length of it, and the insane number of square brackets
One idea I had was to use full-line boundary checks, although that's a hack? (i.e. using ^...$ rather than \b...\b)
Optionally :: Is there a fairly well-established pattern for argument validation in Bash?
^[0-9]{4}$. Don't see^$as a hack btw.