6.1. Using &&

Here’s the general format of &&:

command1 && command2 

The operation of these commands is fairly straightforward. The command on the left of the && (command1) must be returned true (successfully executed) if the command on the right (command2) is to be executed; or to put it another way, ‘If this command works’ && ‘then execute this command’.

Here’s a simple example that illustrates the use of the &&:

						$ cp justice.doc justice.bak && echo "if you are seeing this then cp was 
OK" 

if you are seeing this then cp was OK
					

The copy was successful, so the command on the right of the && (the echo command) was executed.

For a more practical example:

						$ mv /apps/bin /apps/dev/bin && rm -r /apps/bin
					

In the above example, the directory /apps/bin ...

Get Linux and Unix Shell Programming 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.