Prnth
Prnth

Reputation: 143

How to make the following code return to a bash variable?

So I'm trying to execute this:

internet=ping -c 3 google.com | grep -cim1 64;

which would return 1 if the computer is connected to the internet and 0 if it is not. But clearly this does not assign the value to the variable, instead it just echoes it out. How can I assign the value? Also, is there a more intelligent way to check from a bash script if a computer is connected to the internet?

And I would like to stick with ping, and not wget or whatever.

Upvotes: 0

Views: 173

Answers (1)

Barmar
Barmar

Reputation: 781769

$(...) is used to substitute the output of a command:

internet=$(ping -c 3 google.com 2>/dev/null | grep -cim1 64)

Upvotes: 2

Related Questions