0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Validate IP Address with Regex

Use a Bash script with a regex pattern like ^([0-9]{1,3}\.){3}[0-9]{1,3}$ and check each number is between 0 and 255 to validate an IP address.
📋

Examples

Input192.168.1.1
OutputValid IP address
Input256.100.50.25
OutputInvalid IP address
Inputabc.def.ghi.jkl
OutputInvalid IP address
🧠

How to Think About It

To validate an IP address, first check if it has four parts separated by dots using a regex. Then, ensure each part is a number between 0 and 255 because IP address parts cannot be larger than 255.
📐

Algorithm

1
Get the input IP address as a string.
2
Check if the input matches the pattern of four numbers separated by dots.
3
Split the IP address into four parts by the dot separator.
4
For each part, check if it is a number between 0 and 255.
5
If all parts are valid, return that the IP address is valid; otherwise, return invalid.
💻

Code

bash
#!/bin/bash
ip="$1"

if [[ $ip =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
  IFS='.' read -r -a parts <<< "$ip"
  for part in "${parts[@]}"; do
    if (( part < 0 || part > 255 )); then
      echo "Invalid IP address"
      exit 1
    fi
  done
  echo "Valid IP address"
else
  echo "Invalid IP address"
fi
Output
Valid IP address
🔍

Dry Run

Let's trace '192.168.1.1' through the code

1

Regex match

'192.168.1.1' matches the pattern ^([0-9]{1,3}\.){3}[0-9]{1,3}$

2

Split into parts

Split into parts: 192, 168, 1, 1

3

Check each part

All parts are between 0 and 255

PartValueValid Range Check
11920 <= 192 <= 255: true
21680 <= 168 <= 255: true
310 <= 1 <= 255: true
410 <= 1 <= 255: true
💡

Why This Works

Step 1: Regex pattern match

The regex ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ensures the input has exactly four groups of 1 to 3 digits separated by dots.

Step 2: Splitting the IP

Splitting the IP string by dots gives four parts to check individually.

Step 3: Range validation

Each part must be between 0 and 255 because IP address octets cannot exceed this range.

🔄

Alternative Approaches

Using awk
bash
ip="$1"
awk -F'.' 'NF==4 && $1>=0 && $1<=255 && $2>=0 && $2<=255 && $3>=0 && $3<=255 && $4>=0 && $4<=255 {print "Valid IP address"; exit} END {if (NR==0) print "Invalid IP address"}' <<< "$ip"
This method uses awk for inline validation but is less readable for beginners.
Using grep with extended regex
bash
ip="$1"
grep -E '^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$' <<< "$ip" && echo "Valid IP address" || echo "Invalid IP address"
This uses a complex regex to validate ranges directly but is harder to understand.

Complexity: O(1) time, O(1) space

Time Complexity

The script runs in constant time because it processes exactly four parts regardless of input size.

Space Complexity

Uses constant extra space for storing the four parts and temporary variables.

Which Approach is Fastest?

The main script is fast and simple; awk and complex regex methods may be slightly slower and harder to maintain.

ApproachTimeSpaceBest For
Bash regex + numeric checkO(1)O(1)Simple and clear validation
awk inline validationO(1)O(1)Compact but less readable
Complex regex with grepO(1)O(1)Single-step validation but complex
💡
Always check both the format and the numeric range of each IP part for accurate validation.
⚠️
Only checking the format with regex without verifying each number is between 0 and 255.