Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ((0)) cause a Bash script to exit if `set -e` directive is present?

Tags:

bash

shell

This outputs before\n:

#!/usr/bin/env bash
set -e

echo before
((0))
echo after

Removing set -e or changing ((0)) to ((1)) makes the program output before\nafter\n as expected.

Why does ((0)) trigger the set -e exit condition?

like image 285
davidchambers Avatar asked Dec 20 '22 14:12

davidchambers


2 Answers

This will explain:

((0))
echo $?
1

((1))
echo $?
0

So it is due to non-zero return status of arithmetic expression evaluation in (( and )) your script is exiting when set -e is being used.

As help set says this:

-e Exit immediately if a command exits with a non-zero status.

like image 152
anubhava Avatar answered May 14 '23 19:05

anubhava


Line

 set -e

means: -e Exit immediately if a command exits with a non-zero status. (see: https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html)

((0)) is an expression that evaluates to 1. That's why the script exits.

like image 25
Dariusz Avatar answered May 14 '23 18:05

Dariusz