A Simple Linux Shell Script To Display Public IP Of PC

Linux shell scripts are useful for a lot of tasks. A brief tutorial on them can be found here.

Here is a very simple shell script that on executing will display the public IP address of PC.

Using any text editor (nano in this example), make the script as follows :

making shell script using nano text editor

#!/bin/bash

echo "The Public IP for this system is : "
curl -s icanhazip.com

What this script does is simply fetch the public IP address from icanhazip.com.

Accessing icanhazip in a browser will display the public IP too but for this a browser needs to be used every time.

icanhazip displaying public IP in browser

Instead this can be directly done from Linux Terminal, so the script.

Finally, make it executable and run it :

sudo chmod a+x getpublicip.sh
./getpublicip.sh

It will display the public IP of system right from the Linux Terminal.

linux shell script to display public IP

All done.

  1. Thanks for sharing a very specific guide

  2. ludo king says:

    Thank you for your post, I look for such article along time, today i find it finally.
    this post give me lots of advise it is very useful for me !