Display IP address in bash shell prompt

I have seen some other methods but this is my technique using the relatively lightweight sed:

export PS1=`/sbin/ifconfig eth0 | sed -n '0,/^s+inet addr:([0-9]+[.][0-9]+[.][0-9]+[.][0-9]+).*$/s//1/p'`':w$ '

Or you can use whatever interface you want instead of eth0.  If left blank then it will display the first interface.

Avatar for chris

About chris

Software developer
This entry was posted in Tips and tagged , , , . Bookmark the permalink.

3 Responses to Display IP address in bash shell prompt

  1. Avatar for Anonymous Anonymous says:

    Fail. If you have a router it will only show the computer’s internal IP.

    • Avatar for chris chris says:

      That’s the point. Showing the unique internal IP is typically what you want. It’s useful when managing multiple systems so that you know which machine you are logged into.

  2. Avatar for Reggie Reggie says:

    Works beautifully…thankyou!

Leave a Reply

Your email address will not be published.