O'Reilly logo

Linux and Unix Shell Programming by David Tansley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required