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}')

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!

116 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?

    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!

  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.

  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!

  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!

  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

  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

  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?

  8. dani

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

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

  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.

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

  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!

  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!

  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.

  15. happy days

    I needed to send you one very small observation to be able to say thanks the moment again about the splendid tricks you have provided at this time. It is certainly unbelievably generous of you to supply easily all a few people could possibly have supplied as an electronic book to generate some cash on their own, particularly considering the fact that you could possibly have tried it in the event you decided. Those basics as well served to be a great way to be certain that other individuals have a similar fervor like my own to know good deal more on the subject of this issue. I think there are numerous more pleasant times ahead for individuals who find out your website.

  16. dani

    Wonderful work! This is the kind of information that are meant to be shared around the internet. Disgrace on Google for now not positioning this put up higher! Come on over and seek advice from my website . Thank you =)

  17. Marin

    I am glad for commenting to let you be aware of what a superb experience our girl found visiting the blog. She figured out plenty of issues, which included what it’s like to possess an ideal helping heart to make many others easily understand several tortuous subject matter. You really exceeded our own expected results. I appreciate you for providing those beneficial, healthy, revealing as well as fun guidance on this topic to Mary.

  18. kredit mobil honda

    I am beginning a business throughout the summer where I use kids individually or throughout a camp. I was wondering how I could begin a website intended for my clients parents to look at during the summer..

  19. rumah 2 lantai

    Howdy! I’m at work browsing your blog from my new apple iphone! Just wanted to say I love reading your blog and look forward to all your posts! Keep up the fantastic work!

  20. Download Film 2018

    Everybody says you can’t move an initial Tumlbr blog without removing the accounts, but what about a secondary blog? I have two accounts, one which I use and one that I simply started. We don’t use the main blog within the old one particular, but I actually do use a secondary blog on a single account. Will there be any way I could move that secondary blog page to my new accounts as a supplementary blog as well?. Thanks!.

  21. Ronald Rabey

    I simply could not leave your website before suggesting that I extremely loved the usual info an individual provide to your guests? Is gonna be back regularly to check up on new posts

  22. Germaine Biasotti

    I haven¡¦t checked in here for a while because I thought it was getting boring, but the last few posts are good quality so I guess I¡¦ll add you back to my everyday bloglist. You deserve it my friend

  23. Humberto Gaudy

    Thanks for your marvelous posting! I certainly enjoyed reading it, you are a great author.I will make certain to bookmark your blog and will often come back later in life. I want to encourage yourself to continue your great work, have a nice afternoon!

  24. Ramon Estep

    Its like you read my mind! You appear to know so much approximately this, like youwrote the guide in it or something. I feel that you can do with some to power themessage home a little bit, but other than that, this is fantasticblog. A fantastic read. I’ll definitely be back.

  25. Elliot Hindes

    I like what you guys are up also. Such smart work and reporting! Carry on the superb works guys I have incorporated you guys to my blogroll. I think it’ll improve the value of my site 🙂

  26. Luz

    I cherished as much as you’ll obtain performed right here. The sketch is attractive, your authored material stylish. however, you command get got an impatience over that you wish be turning in the following. unwell unquestionably come more formerly once more since precisely the similar nearly a lot frequently within case you defend this increase.

  27. Milan Lachat

    Hello There. I found your blog using msn. This is a very well written article. I will make sure to bookmark it and come back to read more of your useful info. Thanks for the post. I will definitely return.

  28. Jamel

    Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You obviously know what youre talking about, why throw away your intelligence on just posting videos to your weblog when you could be giving us something informative to read?

  29. model baju kebaya

    Pretty great post. I simply stumbled upon your blog and wished to mention that I have really enjoyed browsing your weblog posts. After all I’ll be subscribing to your feed and I hope you write again very soon!

  30. jual beli busur panah

    Want to make people visit a blog page and don’t understand how. Also can you give me a good blogging site that people visit a lot. Do they check out your blog inside the blog site or externally?? Thanks a lot..

  31. Elza Hassenplug

    I have been exploring for a little for any high-quality articles or weblog posts on this sort of area . Exploring in Yahoo I finally stumbled upon this site. Reading this info So i am glad to convey that I’ve an incredibly just right uncanny feeling I came upon just what I needed. I such a lot surely will make sure to don’t omit this site and give it a look regularly.

  32. instacart coupon 2017

    I think this is one of the most significant info for me.
    And i’m glad reading your article. But want to remark on few
    general things, The web site style is ideal,
    the articles is really great : D. Good job, cheers

  33. instacart coupon

    Hey! I just wanted to ask if you ever have any trouble with
    hackers? My last blog (wordpress) was hacked and I ended up losing many months of hard work due to no back up.

    Do you have any methods to protect against hackers?

  34. Rey Procaccini

    Simply wish to say your article is as astonishing. The clarity in your post is simply great, and I could assume you are an expert on this subject. Well with your permission let me grab your RSS feed to keep updated with forthcoming post. Thanks a million and please keep up the gratifying work.

  35. ed sheeran concert

    What i do not realize is in reality how you are no longer actually a lot more neatly-appreciated than you may be right now. You are so intelligent. You understand therefore considerably in relation to this matter, made me personally believe it from a lot of varied angles. Its like women and men don’t seem to be fascinated except it?s something to accomplish with Lady gaga! Your own stuffs excellent. At all times deal with it up!

  36. Furniture Jati

    I simply figured out that my computer has this kind of trojan. I had been running just about all of the Anti virus things my computer offers. The main one says there is nothing at all there and my Home windows defender says I have two trojans therefore I’m not sure what to do. I’m just thinking of buying or creating a new 1.. Right now I have intel. Im not sure which is best like Dell, Miscrosoft, etc . I need a video gaming computer which could also be used as a school work computer. Any kind of ideas?.

  37. cheap custom logo

    My wife and i ended up being very relieved that Ervin managed to finish up his basic research from the precious recommendations he gained while using the web site. It’s not at all simplistic just to find yourself releasing information which men and women could have been making money from. So we fully grasp we have got the blog owner to appreciate for that. All of the illustrations you made, the simple web site navigation, the relationships your site aid to promote – it’s got many great, and it is leading our son in addition to our family know that the concept is fun, which is certainly truly mandatory. Thank you for all the pieces!


Leave a Reply

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