Open In App

Shell Script to List All IP

Last Updated : 05 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

IP addresses can be thought of as house addresses. Just like we have a unique house address for each house, similarly, we have a unique IP address for each unique device on the network. An IP address is used to uniquely identify a device on an internet protocol network.

Example of an IP address:

192.0.2.1/24

Here, 192.0.2.1 is called network ID and 24 is the host ID. We can think of the network ID as a Street name and the host ID is a unique House number.

Writing Shell Script to list all IP addresses

Method 1: Using hostname command:

The hostname is a command which is used to display detailed information about hostname in Linux, hostname is a unique domain name assigned to a computer. You can view your device hostname by writing the hostname command in the terminal. 

Command:

hostname -I

Here, "-I" is used to list all IP addresses.

Output:

Shell Script to List All IP
ip addresses

Let's write a script to list all IP.

After getting all the IP, split these IP addresses by space. Display the formatted output.

Script:

#! /bin/bash

# saving all the ip addresses
ip_addresses=$(hostname -I)

# splitting them by space
ip_addresses=(${ip_addresses//" "/ })

# Print each ip address line by line
echo "LIST OF IP ADDRESSES"
for ip in "${ip_addresses[@]}";
do
printf "$ip\n"
done

Output:

Shell Script to List All IP
List of ip addresses

Note: hostname -I will not display link-local ip6 address because it is automatically configured for every interface that supports IPv6.

Method 2: Using awk command:

awk is a Turing-complete pattern matching programming language widely used with other Linux commands for pattern matching and data extraction.

Script:

#! /bin/bash

# Extracting lines that match the following pattern
ip=$(ip address| awk '/inet/ {print $2}' | grep -v ^::1 | grep -v ^127)

#displaying IP addresses
echo "LIST OF IP ADDRESSES"
echo "$ip"

Here ip address will list the information about networks including their ip addresses, using awk '/inet/ {printf $2}' we're finding patterns that start with inet and printing the addresses. And grep -v ^::1 will invert the match that contains "::1", similarly we don't want to list our localhost as an ip address, so we are omitting it using the grep -v ^127.

Output :

Shell Script to List All IP
List of ip addresses

Conclusion

Bash scripts are a particular kind of shell script, designed to execute in the Bash shell—the most widely used shell in Linux today. Shell script is a more generic term that can be scripted for any shell, such as Bash, Zsh, or Dash. Although all Bash scripts are shell scripts, not all shell scripts are Bash scripts.

If you are in the field of Linux automation, server administration, or command-line scripting, Bash and Shell scripting skills allow you to write more portable, optimized, and compatible scripts.


Next Article
Article Tags :

Similar Reads