Skip to content

EXIT codes in bash

panndabea edited this page Jan 14, 2024 · 6 revisions

Example 1: Executing Commands in Bash Pipeline

  • Command: ls | wc | asdf
  • Result: echo $? is 127

Description:

  1. The command ls lists the contents of the current directory.
  2. The output of ls is piped to the wc command, which counts the number of lines, words, and characters.
  3. The output of wc is then piped to the non-existent command asdf.
  4. Since asdf does not exist, the exit code ($?) is 127, indicating a command error.

Example 2: Executing Commands in Bash Pipeline

  • Command: asdf | ls | wc
  • Result: echo $? is 0

Description:

  1. The non-existent command asdf attempts to pass its output to ls.
  2. The command ls lists the contents of the current directory.
  3. The output of ls is then piped to the wc command, which counts the number of lines, words, and characters.
  4. Since the previous commands were successful, the exit code ($?) is 0, indicating successful execution.

Example 3: Executing Commands in Bash Pipeline

  • Command: ls | asdf | wc
  • Result: echo $? is 0

Description:

  1. The command ls lists the contents of the current directory.
  2. The output of ls is piped to the non-existent command asdf.
  3. Since asdf does not exist, the output of ls is directly piped to the wc command.
  4. Since the previous commands were successful, the exit code ($?) is 0, indicating successful execution.
Clone this wiki locally