Combining Commands with Boolean Operators

If you want to run a second command only if the first command is successfully completed, you can. Every command you issue to the system outputs an exit status, 0 for true (successful) and 1 for false (failed). The system receives these, even if they are not displayed to the user. The && operator, when added to the end of a command, reads that exit status and confirms its value as 0 for true before allowing the next command to be run. Again, the letters represent commands.

matthew@seymour:~$ i && k

You can do the exact opposite with ||, which only runs the following command if the first one returns an exit status of 1 for false.

matthew@seymour:~$ m || n

Get Ubuntu Unleashed 2014 Edition: Covering 13.10 and 14.04,Ninth 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.