I have this script to check for internet connectivity. I use ping for it. Whenever I use ping google.com -c 4 | grep time it produces a non-empty response. Something like 64 bytes from hkg03s11-in-f4.1e100.net ( icmp_req=2 ttl=52 time=90.6 ms. But, when connectivity is down, the time part does not show up, therefore, when I execute the same command, it shows up as empty. How do I use if for it? I'm confused whether to use " " or "".

Which one do I use?

if [ 'ping google.com -c 4 | grep time' != " " ]; then


if [ 'ping google.com -c 4 | grep time' != "" ]; then

Notice the space between the ""

  • 3
    Try echoing $? after running your ping. It'll be different values if it worked or not. – Sobrique Mar 16 '15 at 15:41
  • Use the SECOND choice: "" – mdpc Mar 16 '15 at 18:14

[ … != " " ] compares the left-hand side with a string containing a single space. [ … != "" ] compares the left-hand side with an empty string. Shell programming has oddities and pitfalls, but this particular case is straightforward.

'ping google.com -c 4 | grep time' is a literal 32-character string. You presumably meant to use the output of the command, i.e. a command susbtitution. You can use backquotes for that (instead of the forward quotes that you used), or dollar-parenthesis which is more readable and less error-prone in some complex cases. Either way, you need double quotes around the whole thing, otherwise the result of the command substitution would be split into words and further nastiness.

if [ "$(ping google.com -c 4 | grep time)" != "" ]; then …

All of this is a rather roundabout, brittle way of testing connectivity. Use the return status from ping instead. A single packet is probably enough, and you should specify a fairly small timeout in case the network is down. Pinging google.com tests whether both IP connectivity and DNS are working. You can ping to test whether IPv4 connectivity is working ( is a server run by Google).

if ping -q -c 1 -W 1 >/dev/null; then
  echo "The network is up"
  echo "The network is down"

See also Shell Scripting: Proper way to check for internet connectivity?

  • I should have used a forward quote but the stack exchange markdown acts funny. – PNDA Mar 17 '15 at 9:03

how about

 if ping google.com -c 4
     ... OK
     ... not OK

like most unix command ping will give a return code that can be tested.

To answer Original question, you can't directly write empty string, you should use

if [ x'ping google.com -c 4 | grep time' != x ]; then
  • if ping google.com -c 4 | grep time give empty string x will equal x
  • 3
    You can "write empty string". The problem was with the OP using the wrong types of quotes. It should be (from a syntax point of view) [ "`ping google.com -c 4 | grep time`" != "" ] or better: [ -n "$(ping google.com -c 4 | grep time)" ]. Though of course you'd want if ping... | grep -q time.... – Stéphane Chazelas Mar 16 '15 at 15:41
  • If you're testing command's return code, it's good idea to silent the command output entirely with > /dev/null 2>&1 – Flint Mar 16 '15 at 15:42

Between the two alternatives you have provided, later one is correct:

if [ 'ping google.com -c 4 | grep time' != "" ]; then

You can store something to a variable, say var, and check if it is empty using [ -z "$var ]". If it is empty, its exit status $? would be zero.


$ var="something"
$ [ -z "$var" ]
$ echo $?

$ var=""
$ [ -z "$var" ]
$ echo $?

You can test for internet connectivity, you may use following one liner:

ping  -s 1 -c 1 -W 2 -q www.google.com && echo "SUCCESS" || echo "FAILURE"

With -s 1, we will send only 1 byte of data. With -c 1, we will send only one ICMP packet. With -W 2, we specify that, we are going to wait only for 2 seconds for a ping response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.