Search code examples
bashiphostnameifconfig

Get single IP address without pipe


Centos7 using bash. I need to set an environmental variable, $IP. However, the command to set the variable is done through a company system that expects | to be the delimiter between variables (i.e., I'd pass the system a list in the form myIP=something|myOtherIP=something_else|envVar3=17).

I am planning for a use case where the IP address is always internal (10.x.x.x). Is there any command that can return just the one IP address I care about, without using pipe?

hostname -i returns the wrong address (doesn't start with 10.). hostname -I includes the correct address, but it's second in the list of seven. I can easily get the IP with either hostname -I or ifconfig, but it involves piping to grep and then using regex to find the one that starts with 10. I doubt there's any easy way (honestly, I doubt it's possible), but I figured I'd ask anyway.


Solution

  • something like this?

    grep -o  "10[^ ]*"<<<`hostname -I`