Shell Script to List All IP
In a layman’s language, 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:

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:

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 :

List of ip addresses