I'm trying to use just the IP address (inet) as a parameter in a script I wrote.
Is there an easy way in a unix terminal to get just the IP address, rather than looking through ifconfig
?
I'm trying to use just the IP address (inet) as a parameter in a script I wrote.
Is there an easy way in a unix terminal to get just the IP address, rather than looking through ifconfig
?
You can write a script that only return the IP like:
For MAC:
Or for linux system
I always wind up needing this at the most unexpected times and, without fail, wind up searching for threads like this on SO. So I wrote a simple script to get IPv4 addresses via netstat, called
echoip
- you can find it here. The bash for network addresses looks like this, it also gets your public address from ipecho.net:The
echoip
script yields an output like this:On latest Ubuntu versions (14.04 - 16.04), this command did the trick for me.
I would Use
Hostname -L
to get just the IP to use as a variable in a script.We can simply use only 2 commands ( ifconfig + awk ) to get just the IP (v4) we want like so:
On Linux, assuming to get IP address from
eth0
interface, run the following command:On OSX, assumming to get IP affffdress from
en0
interface, run the following command:In
man hostname
there is even more easier way which automatically excluding loopback IP and showing only space separated list of all assigned to host ip addresses: