The Execution Flow of Commands

7 min read·Jan 1, 2025

In programming, it often happens that a certain task requires the execution of multiple successive commands, which can be executed either sequentially or conditionally.

Sequential execution refers to the process of executing multiple commands, one after the other, in the order they are written.

Conditional execution, on the other hand, refers to the process of executing or skipping commands based on pre-determined conditions.

Executing commands sequentially

In Bash, there are essentially two ways to executes commands sequentially.

You can either type and execute one command at a time, awaiting for the prompt to reappear between each execution:

$ command1
$ command2
$ command3

Or you can type all the commands in bulk on a single line, separated by a semicolon command separator ;, and automatically execute all of them in the order they are written:

$ command1 ; command2 ; command3

Example

In this example, we are manually executing the echo command twice, one after the other:

$ echo Hello
Hello
$ echo World
World
$

In this example, we are delegating the sequential execution of the echo commands to the shell:

$ echo Hello ; echo World
Hello
World
$

Note: When executing commands in bulk, the shell will not display the command prompt between each execution.

Executing commands conditionally

When running multiple commands in a sequence, it is sometimes useful to be able to control the execution of a command based on the success or failure of the previous one.

For example, if we wanted to create a new folder and within it a new file, we would need to first make sure that the folder was successfully created before attempting to create the file.

The exit status

The exit status, also called return value, is an integer value that is sent by a program to the shell at the end of its execution.

By convention, a program will return the value 0 if everything went well, and another value between 1 and 255 if something went wrong.

This value is stored in a special character sequence $? that can be displayed in the terminal using the echo command.

Note: The $? variable only holds the exit status of the last command executed.

Example

In this example, we can see that the execution of the first echo command was successful as the "Hello" word was printed in the terminal window and its exit status equals to 0:

$ echo Hello
Hello
$ echo $?
0

Logical operators

Logical operators are symbols or words used to perform logical operations in order to control the flow of a program based on conditions.

They are primarily used in Boolean algebra, which deals with true and false values, also known as boolean values.

In Bash, the two logical operators are:

  • &&: the logical AND.
  • ||: the logical OR.

Note: In Bash, the true and false expressions are respectively interpreted by the shell as the values 0 and 1.

The logical && operator

When using the && operator, the right side expression will only be evaluated by the shell if the exit status of the left side expression is 0.

$ command1 && command2

This means that command2 will only be executed if command1 succeeds.

The logical || operator

When using the || operator, the right side expression will only be evaluated by the shell if the exit status of the left side expression is different from 0.

$ command1 || command2

This means that command2 will only be executed if command1 fails.

Example

In this example, the word "Hello" is successfully displayed in the terminal as the left side expression evaluates to 0.

$ true && echo Hello
Hello

In this example, the echo command is not executed as the left side expression evaluates to 1.

$ false && echo Hello

In this example, the word "Hello" is successfully displayed in the terminal as the left side expression evaluates to 1.

$ false || echo Hello
Hello

In this example, the echo command is not executed as the left side expression evaluates to 0.

$ true || echo Hello

Note: Command options and arguments, command separators, and logical operators are not mutually exclusive and can be combined altogether.

For example:

$ true && echo -n one ; echo two ; false || echo three

Summary

Here's a summary of what you've learned in this lesson:

  • Commands can be executed sequentially using the semicolon command separator (;).
  • The exit status is a numeric value returned by a program at the end of its execution.
  • Commands can be executed conditionally using the logical AND (&&) and OR (||).

Enjoying the courses?

I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:

Support BackendBrewery
The Execution Flow of Commands in Bash | Backend Brewery