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


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

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

linux shell script to display public IP

Alternatively, replace incanhazip with eth0.me in the script, the result will be the same.


echo "The Public IP for this system is : "
curl -s eth0.me

All done.

  1. 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 !

Leave a Comment