Skip to main content
Tweeted twitter.com/StackUnix/status/911270745774403584
Post Reopened by Kusalananda bash
restructuring the question to fit the actual problem better
Source Link
Minix
  • 6.1k
  • 8
  • 31
  • 46

Why is set -e ignored, when using adoes this compound command {...} not exit on error, when used with ||?

I just came upon some shell behaviour I don't understand. TakeI'm trying to run this commandscript:

~$ #!/bin/bash -e
~$  
{ echo 
 "Hello"; false;  echo "World";"Doing }

As expected, the output is:

Hellosomething"; 

After which bash exits with return code 1.

But, when I modify the command like this:

~$ bash -e
~$ { echowill_fail "Hello"; false;               # like `false` 

    echo "World";"Worked";  
} || echo "Mars"

I would assume the output would be:

Hello
Mars

Instead it is:

Hello
World"Failed"

And the bash shell is still functional. It seems like || catches the error code of falseTo my surprise, so that -ewill_fail doesn't kill the bash shellfailed, but I openeddid not see "Failed" on my command line, but then instead of executing echo "Mars" it just continues after false"Worked".

What givesWhy did the compound command not exit with error after will_fail failed?

Why is set -e ignored, when using a compound command {...}?

I just came upon some shell behaviour I don't understand. Take this command:

~$ bash -e
~$ { echo "Hello"; false; echo "World"; }

As expected, the output is:

Hello

After which bash exits with return code 1.

But, when I modify the command like this:

~$ bash -e
~$ { echo "Hello"; false; echo "World"; } || echo "Mars"

I would assume the output would be:

Hello
Mars

Instead it is:

Hello
World

And the bash shell is still functional. It seems like || catches the error code of false, so that -e doesn't kill the bash shell I opened, but then instead of executing echo "Mars" it just continues after false.

What gives?

Why does this compound command {...} not exit on error, when used with ||?

I'm trying to run this script:

#!/bin/bash -e
 
{  
    echo "Doing something"; 
    will_fail                 # like `false` 

    echo "Worked";  
} || echo "Failed"

To my surprise, will_fail failed, but I did not see "Failed" on my command line, but "Worked".

Why did the compound command not exit with error after will_fail failed?

edited title
Link
Minix
  • 6.1k
  • 8
  • 31
  • 46

Why is set -e ignored in this case, when using a compound command {...}?

Post Closed as "Duplicate" by cuonglm bash
Source Link
Minix
  • 6.1k
  • 8
  • 31
  • 46

Why is set -e ignored in this case?

I just came upon some shell behaviour I don't understand. Take this command:

~$ bash -e
~$ { echo "Hello"; false; echo "World"; }

As expected, the output is:

Hello

After which bash exits with return code 1.

But, when I modify the command like this:

~$ bash -e
~$ { echo "Hello"; false; echo "World"; } || echo "Mars"

I would assume the output would be:

Hello
Mars

Instead it is:

Hello
World

And the bash shell is still functional. It seems like || catches the error code of false, so that -e doesn't kill the bash shell I opened, but then instead of executing echo "Mars" it just continues after false.

What gives?