Putting IP Address into bash variable. Is there a better way

前端 未结 18 1861
生来不讨喜
生来不讨喜 2020-12-07 20:09

I\'m trying to find a short and robust way to put my IP address into a bash variable and was curious if there was an easier way to do this. This is how I am currently doing

18条回答
  •  长情又很酷
    2020-12-07 20:59

    ip is the right tool to use as ifconfig has been deprecated for some time now. Here's an awk/sed/grep-free command that's significantly faster than any of the others posted here!:

    ip=$(ip -f inet -o addr show eth0|cut -d\  -f 7 | cut -d/ -f 1)
    

    (yes that is an escaped space after the first -d)

提交回复
热议问题