Bash has the
=~ regular expression matching operator. Here is an example of using it:
#!/bin/bash input=$1 if [[ "$input" =~ "[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]" ]] # ^ NOTE: Quoting not necessary, as of version 3.2 of Bash. # NNN-NN-NNNN (where each N is a digit). then echo "Social Security number." # Process SSN. else echo "Not a Social Security number!" # Or, ask for corrected input. fi
For negation, all the examples I have seen rely on an
else clause. For situations where I am only interested in negative matches, is there a better way that making a non-functional
For example, if I only cared about the case where the above input is not a Not a Social Security number, what is the right way to go about this?