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

前端 未结 18 1860
生来不讨喜
生来不讨喜 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:47

    man hostname recommends using the --all-ip-addresses flag (shorthand -I ), instead of -i, because -i works only if the host name can be resolved. So here it is:

    hostname  -I
    

    And if you are interested only in the primary one, cut it:

    hostname  -I | cut -f1 -d' '
    

提交回复
热议问题