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
?
I wanted something simple that worked as a Bash alias. I found that
hostname -I
works best for me (hostname v3.15).hostname -i
returns the loopback IP, for some reason, buthostname -I
gives me the correct IP for wlan0, and without having to pipe output through grep or awk. A drawback is thathostname -I
will output all IPs, if you have more than one.use this one line script:
ifconfig | grep "inet " | grep -v 127.0.0.1|awk 'match($0, /([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/) {print substr($0,RSTART,RLENGTH)}'
mac & linux (tested in ubuntu) both works.This will give you all IPv4 interfaces, including the loopback 127.0.0.1:
This will only show
eth0
:And this way you can get IPv6 addresses:
Only
eth0
IPv6:Command
ifconfig
is deprected and you should useip
command on Linux.Also
ip a
will give you scope on the same line as IP so it's easier to use.This command will show you your global (external) IP:
All IPv4 (also 127.0.0.1):
All IPv6 (also ::1):
That would do the trick in a Mac :
That resolved to
ping 192.168.1.2
in my machine.Pro tip:
$(...)
means run whatever is inside the parentheses in a subshell and return that as the value.shows all your ips