Local IP Address in Bash

How to get your local IP address in Bash

There are many ways to get your local IP address in Linux, and even more in Bash. If you know of one not mentioned here, please comment and mention it so I can add it. I’m putting this information here for future reference.

So, how do you get the local IP address on linux? Linux has a command-line command called “ifconfig” which is, basically, the linux version of Window’s “ipconfig”. Add some standard Linux commands to the mix and you can easily parse all that complicated output to give you just the local IP address for use in your Bash script. Here’s the full command:

echo $(ifconfig eth0 | grep "inet addr:" | cut --delimiter=: --fields=2 | awk '{print $1}')

If you aren’t familiar with these commands, here’s a quick overview:

  • echo” simply prints out whatever you pass to it. In this case, it will be the result of this “one liner” script.
  • ifconfig eth0″ prints out just the information about your “eth0” connection.
  • grep” widdles down our output even further, filtering out everything except the line that has our ip address in it.
  • cut” essentially splits our 1 string into an array of several strings.
  • awk” is a scripting language of it’s own, often used in Linux with a myriad of uses. In this script we’re using it to print the first string in our array (since that’s the IP address that we’re after). A note about Awk’s print function: while “$0” is valid, print is not zero-based. “$0” will print the whole line, while “$1” will print the first item.

And there you have it! Running the above command would give you the local IP address of the Linux computer you’re working on, like so…

chrisanesbit@ubuntu-rocks:~$ echo $(ifconfig eth0 | grep "inet addr:" | cut --delimiter=: --fields=2 | awk '{print $1}')
chrisanesbit@ubuntu-rocks:~$ 192.168.0.10

And “as is” that command can be dropped right into your very own Bash script. Simply assign it to a variable instead of echoing it to StdOut and you’ve got yourself a local IP to script against!

Know of another way, maybe even a better way, to accomplish this? Leave me a comment and let me know!

55 thoughts on “Local IP Address in Bash

  1. rjc

    You can drop `grep` altogether:

    ifconfig eth0 | awk ‘/inet addr:/ { print $2 ; }’ | cut -d : -f 2

    Also, why are you using`echo` with a sub-shell at all?

    Reply
    1. cnesbit Post author

      hey rjc!

      Nice tip! a little more concise, especially since I was using awk anyhow.

      As to my use of echo, while my example was being performed on the command line, the intent was to show how to display the local IP from a bash script (in which case, the echo becomes necessary). I see how that’s confusing since my example is in a different context, so I will update this post accordingly.

      Thanks for your feedback! I hope you find the information on my blog helpful!

      Reply
  2. education for all

    I loved as much as you’ll receive carried out right here. The sketch is tasteful, your authored material stylish. nonetheless, you command get bought an nervousness over that you wish be delivering the following. unwell unquestionably come further formerly again since exactly the same nearly a lot often inside case you shield this hike.
    http://learninghints.eu

    Reply
  3. financial consultant

    Very good website you have here but I was curious if you knew of any discussion boards that cover the same topics talked about in this article? I’d really like to be a part of group where I can get feed-back from other knowledgeable people that share the same interest. If you have any recommendations, please let me know. Kudos!
    http://financehints.eu

    Reply
  4. homeschooling

    You actually make it seem so easy with your presentation but I find this matter to be really something which I think I would never understand. It seems too complicated and very broad for me. I am looking forward for your next post, I’ll try to get the hang of it!
    http://studytip.eu

    Reply
  5. Kata Kata Ucapan Lebaran Idul Fitri 2017/2018

    921169 439249Water-resistant our wales in advance of when numerous planking. The specific wales surely are a selection of heavy duty snowboards that this height ones would be exactly the same in principle as a new shell planking having said that with significantly a lot more height to assist you thrust outward within the evening planking. planking 496337

    Reply
  6. roofing

    468142 496428There is numerous separate years Los angeles Weight reduction eating plan with each a person is a necessity. The pioneer part can be your original getting rid of belonging to the extra pounds. la weight loss 645367

    Reply
  7. Brooks

    magnificent issues altogether, you just gained a emblem new reader. What may you recommend about your publish that you made some days ago? Any certain?

    Reply
  8. dani

    “Greetings! Very helpful advice within this post! It is the little changes which will make the greatest changes. Thanks for sharing!”

    Reply
  9. Warna rambut untuk kulit sawo matang

    Flash is already installed on my work pc, however chrome is not really communicating with this for some unusual reason. I can not reinstall adobe flash because like i stated this is my works computer, therefore i need admin rights to install display. So how can i make Firefox work with the flash that is already been installed by admin?.. thanks!.

    Reply
  10. Angelo Shinoda

    I would like to thnkx for the efforts you’ve put in writing this blog. I am hoping the same high-grade site post from you in the upcoming as well. Actually your creative writing skills has encouraged me to get my own blog now. Actually the blogging is spreading its wings quickly. Your write up is a great example of it.

    Reply
  11. Jual Pemancar Radio FM Murah

    Just how long may my computer keep working with an outdated internet browser?. I internet browser my computer is obsolete. However my computer’s little outdated too, and does not have what it would need to run the newest browser. Must i start saving for a fresh one, or can this problem be dealt with for a few even more years yet?.. By the way, I love my computer I don’t want to start over since I love all the programs upon my pc..

    Reply
  12. gambar pemandangan yang indah

    Hey there would you mind letting me know which hosting company you’re using? I’ve loaded your blog in 3 completely different web browsers and I must say this blog loads a lot quicker then most. Can you recommend a good hosting provider at a reasonable price? Thanks a lot, I appreciate it!

    Reply
  13. Maria Malech

    I think other website proprietors should take this website as an model, very clean and great user genial style and design, as well as the content. You are an expert in this topic!

    Reply
  14. Leisa Bult

    whoah this blog is wonderful i love reading your articles. Keep up the good work! You know, a lot of people are searching around for this info, you can help them greatly.

    Reply

Leave a Reply

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