Stopping a command list on error

Most of the time when programming in Bash, you will not actually want to test $? directly, but instead test it implicitly as success or failure, with language features in Bash itself.

If you wanted to issue a set of commands on one command line, but only to continue if every command worked, you would use the double-ampersand (&&) control operator, instead of the semicolon (;):

$ cd && rmdir ~/nonexistent && ls

When we run this command line, we see that the final ls never runs, because the rmdir command before it failed:

rmdir: failed to remove '/home/user/nonexistent': No such file or directory

Similarly, if we changed the cd command at the start of the command line to change into a directory that didn't exist, ...

Get Bash Quick Start Guide 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.