US postal 5 digit zip code regex validation

The purpose of this tutorial is to show how to use a Bash shell script or Python programming code to validate a United States zip code number on a Linux system.

Check out the example scripts below to validate US zip codes from the Linux command line.

In this tutorial you will learn:

  • How to use Bash to validate US zip codes
  • How to use Python to validate US zip codes
US postal 5 digit zip code regex validation
US postal 5 digit zip code regex validation
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux system
Software Bash, Python
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Regex to validate US zip code – Bash and Python examples




As you may have guessed, many different programming langauges could be used to validate a zip code in Linux. US zip codes can contain either five digits total, or five digits followed by a dash and four additional digits. A script just needs to use regular expression to make sure that those requirements are met. In other words, numbers like:

Examples of valid numbers:

  • 32344
  • 32344-4444

Examples of invalid numbers:

  • 323445-44
  • 323445

Two of the most popular tools for the task on Linux are the Bash shell and Python programming language. Both of these are capable of checking zip codes with only a few short lines of code. Use your preferred method below and copy our script examples to your own system to start validating zip codes.

Bash example to validate zip codes

Bash script:

#!/bin/bash

# regexp to get a valid US postal code
echo $1 | grep -qE '^[0-9]{5}(-[0-9]{4})?$'

if [ $? -eq 0 ]; then
	echo "$1 is a valid US postal code."
else
	echo "$1 is an invalid US postal code."
fi

Execution of Bash script:

$ chmod +x validate_us_postal_code.sh

$ ./validate_us_postal_code.sh 32344-4444
32344-4444 is a valid US postal code.

$ ./validate_us_postal_code.sh 32344
32344 is a valid US postal code.

$ ./validate_us_postal_code.sh 323445-44
323445-44 is an invalid US postal code.

$ ./validate_us_postal_code.sh 323445
323445 is an invalid US postal code.

Python example to validate zip codes

Python script:

import re
zip_list=['32344-4444',
'32344',
'323445-44',
'323445']
pattern = '^[0-9]{5}(-[0-9]{4})?$'
for eachnumber in zip_list:
    result = re.match(pattern, eachnumber)
    if result:
        print(eachnumber+" is a valid US postal code.")
    else:
        print(eachnumber+" is an invalid US postal code.")

Execution of Python script:

$ python3 validate_us_zip_code.py

32344-4444 is a valid US postal code.

32344 is a valid US postal code.

323445-44 is an invalid US postal code.

323445 is an invalid US postal code.

Closing Thoughts




In this tutorial, we learned how to use a Bash or Python script to validate zip codes on Linux. Since only regular expression is used, this ends up being a pretty simple job for either programming language. And, of course, many other programming languages are capable of the task.



Comments and Discussions
Linux Forum