For automation, cron jobs, or IoT devices, you often need to verify internet connectivity before continuing. Here are three bash methods to do it. Method 1: ping ping -c 1 sends one packet.

Architecture and Core Concepts

> /dev/null 2>&1 hides output. $? checks the exit status. Method 2: curl Checks whether the HTTP headers respond with a success code. Method 3: wget (silent) Use Cases Startup scripts waiting for network before syncing.

Execution Syntax and Code Implementation

Terminalbash
curl Checks whether the HTTP headers respond with a success code
Terminalbash
wget (silent) Use Cases Startup scripts waiting for network before syncing

Gotchas and Troubleshooting Checklist

  • Permission & Access Control - verify user privilege level (sudo access or file ownership) before running commands.

  • Environment & Path Resolution - confirm environment variables and binary PATH entries match target software releases.

  • Log Diagnostics - inspect relevant system logs or application trace outputs to verify clean execution.

Following these steps provides a clean, reliable, and production-ready solution for check internet connection using a bash script.