Bash - Check internet connection

By xngo on June 17, 2019

There are multiple ways to check whether or not you got internet connection. The most reliable way that I found is to use the following code.

# Check for internet connection.
if wget -q --spider "http://google.com"; then
    echo "Success, got internet connection!"
else
    echo "Error: No internet connection!"
fi

Output

Success, got internet connection!

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.