I am getting my IP address using a curl command, and I want to save it as a shell variable.
I use the following command to get the ip address
curl ipinfo.io/ip
And I assign the variable thusly:
IPADDR=`curl ipinfo.io/ip`
but when I echo this, I get the following:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 14 100 14 0 0 41 0 --:--:-- --:--:-- --:--:-- 164
24.18.247.198
All I want is 24.18.247.198. Any thoughts?
curl provides an attribute to operate "muted": -s
Hence, you can set your variable in this way:
IPADDR=$(curl -s ipinfo.io/ip)
Do like this:
IPADDR=$(curl ipinfo.io/ip 2>/dev/null)
That is, the "% Total", "% Received" and others are printed on stderr. By redirecting stderr to /dev/null
you can get rid of that noise.
Always use $(...)
instead of `...`
when possible.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.