1

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?

2 Answers 2

3

curl provides an attribute to operate "muted": -s

Hence, you can set your variable in this way:

IPADDR=$(curl -s ipinfo.io/ip)
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.