Bash script to loop through values in a file with space as a separator

Lets say we have a file with list of IPs that are space separated and you want to read each of the values to pass to a loop to perform an operation. Here is an example file with IP Addresses separated  by a space:

cat ips.txt
192.168.1.1 192.168.1.10

Now, lets say you want to loop through these IPs and run a ping command against each of them.

cat ping.sh
#!/bin/bash

# IFS is an internal bash variable. Here, we set its value as space.
IFS=$" "
# Read the file "ips.txt" and store the list of values to a variable "ips"
ips="$(cat ips.txt)"

# Run the following loop which will loop through each of the ips and run a ping test
for ip in $ips; do ping -c 1 $ip; done
# Unset the IFS variable so that it wont mess with the reset of the script
unset IFS

-Running this loop, will loop through the list of IP addresses and perform a ping.

./ping.sh
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.660 ms

--- 192.168.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.660/0.660/0.660/0.000 ms
PING 192.168.1.10 (192.168.1.10) 56(84) bytes of data.
64 bytes from 192.168.1.10: icmp_seq=1 ttl=64 time=0.108 ms

--- 192.168.1.10 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.108/0.108/0.108/0.000 ms

Hope this helps!

Happy scripting folks! 🙂

Source/References: Link

3 thoughts on “Bash script to loop through values in a file with space as a separator”

  1. Cool! Thanks for the post!

    In addition to this, try the “parallel” package as well. Rather than looping through a list one by one it can process things in parallel thus speeding up things in computers with multiple cores of processors.

    cat IPS.txt | tr ‘ ‘ ‘\n’ | parallel “ping -c 1 {}”

    The above command translates the space separated list to newline separated list and passes it into parallel. Which then executes ping on each line in a seperate process. The number of processes at the same time is automatically chosen based on the number of processor cores.

  2. $ cat ips.txt;echo;echo;for ip in $(cat ips.txt);do ping -c 1 “${ip}”;done
    192.168.0.18 192.168.0.19

    PING 192.168.0.18 (192.168.0.18) 56(84) bytes of data.
    64 bytes from 192.168.0.18: icmp_seq=1 ttl=64 time=0.229 ms

    — 192.168.0.18 ping statistics —
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 0.229/0.229/0.229/0.000 ms
    PING 192.168.0.19 (192.168.0.19) 56(84) bytes of data.
    64 bytes from 192.168.0.19: icmp_seq=1 ttl=64 time=0.247 ms

    — 192.168.0.19 ping statistics —
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 0.247/0.247/0.247/0.000 ms

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.