[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: set -e not working as expected with conditional operators
From: |
Chet Ramey |
Subject: |
Re: set -e not working as expected with conditional operators |
Date: |
Fri, 2 Jun 2023 09:01:11 -0400 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 |
On 6/1/23 9:01 PM, rpaufin1 wrote:
Consider the following script:
#!/bin/bash
f() {
ls missing-file 2> /dev/null
echo "test"
}
# 1
(set -e; f); ret=$?
if (( ret )); then
echo "failed"
exit 1
fi
# 2
(set -e; f) || {
echo "failed"
exit 1
}
Running the block labelled '1' prints "failed" and returns 1 as the exit code, while
running the other block prints "test" and returns 0. However, the result should be the
same.
It should not. The manual lists the instances where the shell does not
exit if a command fails while -e is enabled, and and-or lists are one of
those.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet@case.edu http://tiswww.cwru.edu/~chet/