Skip to content

EXIT codes in bash

panndabea edited this page Jan 14, 2024 · 6 revisions

Bash Pipeline Examples

Example 1: Try these Commands in Bash Pipeline

  • Command: ls | wc | asdf
  • Result: echo $? is 127
Details and Quick Tip
  1. The ls command lists the contents of the current directory.
  2. The output of ls is piped to the wc command, counting lines, words, and characters.
  3. The result is then piped to the non-existent command asdf.
  4. Since asdf is not found, the exit code ($?) is 127, indicating a command error.

Quick Tip: Copy and paste the command into your terminal to see the outcome!

Example 2: Try these Commands in Bash Pipeline

  • Command: asdf | ls | wc
  • Result: echo $? is 0
Details and Quick Tip
  1. The non-existent command asdf attempts to pass its output to ls.
  2. ls then lists the contents of the current directory.
  3. The output of ls is piped to the wc command, counting lines, words, and characters.
  4. As the previous commands were successful, the exit code ($?) is 0, indicating success.

Quick Tip: Copy and paste the command into your terminal to observe the result!

Example 3: Try these Commands in Bash Pipeline

  • Command: ls | asdf | wc
  • Result: echo $? is 0
Details and Quick Tip
  1. The ls command lists the contents of the current directory.
  2. The output of ls is piped to the non-existent command asdf.
  3. As asdf is missing, the output of ls is directly piped to the wc command.
  4. The exit code ($?) is 0, indicating successful execution of the previous commands.

Quick Tip: Copy and paste the command into your terminal for a hands-on experience!