The null command

In many situations, we may need a command that does nothing and returns a success status such as 0. In such cases, we can use the null command. It is represented by a colon (:). For example, in the if loop, we do not want to add any command if it is successful, but we have certain commands to execute if it fails. In such situations, we can use the null command. This is illustrated in the following if_19.sh script. If we want to loop for ever, then the null command can be used in the for loop:

#!/bin/bash 
city=London 
if grep "$city" city_database_file >& /dev/null 
then 
      : 
else 
      echo "City is not found in city_database_file " 
      exit 1 
fi 

Let's test the program:

    $ chmod +x if_19.sh
    $ ./if_19.sh
  

The following will be the output ...

Get Learning Linux Shell Scripting - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.