0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Find IP Address Easily

Use the Bash command ip addr show | grep 'inet ' | awk '{print $2}' | cut -d/ -f1 to find your IP address in a script.
📋

Examples

InputMachine with IP 192.168.1.10
Output192.168.1.10
InputMachine with IP 10.0.0.5
Output10.0.0.5
InputMachine with multiple IPs 127.0.0.1 and 192.168.0.2
Output127.0.0.1 192.168.0.2
🧠

How to Think About It

To find the IP address, the script looks at the network interfaces and extracts the lines containing IP info. It then picks out the IP numbers by splitting the text and removing extra parts like subnet masks.
📐

Algorithm

1
Run a command to list all network interfaces and their details.
2
Filter the output to keep only lines that show IP addresses.
3
Extract the IP address part from each line by splitting on spaces and slashes.
4
Print the extracted IP addresses.
💻

Code

bash
#!/bin/bash
ip addr show | grep 'inet ' | awk '{print $2}' | cut -d/ -f1
Output
192.168.1.10
🔍

Dry Run

Let's trace the command on a machine with IP 192.168.1.10

1

List interfaces

ip addr show outputs lines including 'inet 192.168.1.10/24'

2

Filter inet lines

grep 'inet ' keeps lines with IP addresses like 'inet 192.168.1.10/24'

3

Extract IP

awk '{print $2}' gets '192.168.1.10/24', cut -d/ -f1 trims to '192.168.1.10'

Command Step
ip addr show
grep 'inet '
awk '{print $2}'
cut -d/ -f1
💡

Why This Works

Step 1: Get network info

The ip addr show command lists all network interfaces and their details including IP addresses.

Step 2: Filter IP lines

Using grep 'inet ' selects only lines that contain IPv4 addresses.

Step 3: Extract IP address

The awk '{print $2}' command picks the second word which is the IP with subnet, and cut -d/ -f1 removes the subnet mask, leaving just the IP.

🔄

Alternative Approaches

Using hostname command
bash
hostname -I | awk '{print $1}'
This is simpler but may return multiple IPs; good for quick scripts.
Using ifconfig (legacy)
bash
ifconfig | grep 'inet ' | awk '{print $2}'
Works on older systems but <code>ifconfig</code> is deprecated on many modern Linux distros.
Using ip command with JSON output
bash
ip -j addr show | jq -r '.[].addr_info[] | select(.family=="inet") | .local'
Requires <code>jq</code> tool; outputs clean JSON-parsed IP addresses.

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

Time Complexity

The commands process each line of the network info once, so time grows linearly with the number of interfaces.

Space Complexity

The script uses minimal extra memory, only storing small parts of the output at a time.

Which Approach is Fastest?

Using hostname -I is fastest and simplest but less flexible; parsing ip addr show is more robust for detailed info.

ApproachTimeSpaceBest For
ip addr show + grep + awk + cutO(n)O(1)Detailed and reliable IP extraction
hostname -IO(1)O(1)Quick and simple IP list
ifconfig + grep + awkO(n)O(1)Legacy systems compatibility
ip -j addr show + jqO(n)O(n)Structured JSON output, needs jq
💡
Use hostname -I for a quick way to get all IP addresses in one line.
⚠️
Beginners often forget to remove the subnet mask, resulting in IP addresses like '192.168.1.10/24' instead of just '192.168.1.10'.