Why does ! operator with set -e always succeed even for truthy return values?
I use a system where scripts utilize set -e
:
#!/bin/sh
set -e
echo 1
true
echo 2
true
echo 3
false
echo notreached
If I do this:
#!/bin/sh
set -e
echo 1
true
echo 2
true
echo 3
! false
echo reached
...the last line is reached. However, if I do this:
#!/bin/sh
set -e
echo 1
true
echo 2
true
echo 3
! true
echo reached
...the last line is reached even though ! true
should be false
.
More testing:
user@linux:~$ ! false && echo ok
ok
user@linux:~$ ! true && echo ok
user@linux:~$
...so in these cases, the !
operator works correctly.
However, it does not work properly with set -e
. Why?
Related: http://mywiki.wooledge.org/BashFAQ/105 ...although that does not answer my question.
bash shell-script
add a comment |
I use a system where scripts utilize set -e
:
#!/bin/sh
set -e
echo 1
true
echo 2
true
echo 3
false
echo notreached
If I do this:
#!/bin/sh
set -e
echo 1
true
echo 2
true
echo 3
! false
echo reached
...the last line is reached. However, if I do this:
#!/bin/sh
set -e
echo 1
true
echo 2
true
echo 3
! true
echo reached
...the last line is reached even though ! true
should be false
.
More testing:
user@linux:~$ ! false && echo ok
ok
user@linux:~$ ! true && echo ok
user@linux:~$
...so in these cases, the !
operator works correctly.
However, it does not work properly with set -e
. Why?
Related: http://mywiki.wooledge.org/BashFAQ/105 ...although that does not answer my question.
bash shell-script
add a comment |
I use a system where scripts utilize set -e
:
#!/bin/sh
set -e
echo 1
true
echo 2
true
echo 3
false
echo notreached
If I do this:
#!/bin/sh
set -e
echo 1
true
echo 2
true
echo 3
! false
echo reached
...the last line is reached. However, if I do this:
#!/bin/sh
set -e
echo 1
true
echo 2
true
echo 3
! true
echo reached
...the last line is reached even though ! true
should be false
.
More testing:
user@linux:~$ ! false && echo ok
ok
user@linux:~$ ! true && echo ok
user@linux:~$
...so in these cases, the !
operator works correctly.
However, it does not work properly with set -e
. Why?
Related: http://mywiki.wooledge.org/BashFAQ/105 ...although that does not answer my question.
bash shell-script
I use a system where scripts utilize set -e
:
#!/bin/sh
set -e
echo 1
true
echo 2
true
echo 3
false
echo notreached
If I do this:
#!/bin/sh
set -e
echo 1
true
echo 2
true
echo 3
! false
echo reached
...the last line is reached. However, if I do this:
#!/bin/sh
set -e
echo 1
true
echo 2
true
echo 3
! true
echo reached
...the last line is reached even though ! true
should be false
.
More testing:
user@linux:~$ ! false && echo ok
ok
user@linux:~$ ! true && echo ok
user@linux:~$
...so in these cases, the !
operator works correctly.
However, it does not work properly with set -e
. Why?
Related: http://mywiki.wooledge.org/BashFAQ/105 ...although that does not answer my question.
bash shell-script
bash shell-script
asked 1 hour ago
juhistjuhist
1333
1333
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Because that's what the standard says:
-e
When this option is on, when any command fails, the shell immediately shall exit, [...], with the following exceptions:
- The -e setting shall be ignored when executing the compound list following the while, until, if, or elif reserved word, a pipeline
beginning with the ! reserved word, or any command of an AND-OR list
other than the last.
http://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/V3_chap02.html#set
Bash's manual also points this out, with somewhat different wording.
I think that entry in BashFAQ doesn't even try to spell out the specifics, the phrase "These rules are extremely convoluted" makes that pretty clear. Though the linked pages seem to have more than one would want to know.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496316%2fwhy-does-operator-with-set-e-always-succeed-even-for-truthy-return-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Because that's what the standard says:
-e
When this option is on, when any command fails, the shell immediately shall exit, [...], with the following exceptions:
- The -e setting shall be ignored when executing the compound list following the while, until, if, or elif reserved word, a pipeline
beginning with the ! reserved word, or any command of an AND-OR list
other than the last.
http://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/V3_chap02.html#set
Bash's manual also points this out, with somewhat different wording.
I think that entry in BashFAQ doesn't even try to spell out the specifics, the phrase "These rules are extremely convoluted" makes that pretty clear. Though the linked pages seem to have more than one would want to know.
add a comment |
Because that's what the standard says:
-e
When this option is on, when any command fails, the shell immediately shall exit, [...], with the following exceptions:
- The -e setting shall be ignored when executing the compound list following the while, until, if, or elif reserved word, a pipeline
beginning with the ! reserved word, or any command of an AND-OR list
other than the last.
http://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/V3_chap02.html#set
Bash's manual also points this out, with somewhat different wording.
I think that entry in BashFAQ doesn't even try to spell out the specifics, the phrase "These rules are extremely convoluted" makes that pretty clear. Though the linked pages seem to have more than one would want to know.
add a comment |
Because that's what the standard says:
-e
When this option is on, when any command fails, the shell immediately shall exit, [...], with the following exceptions:
- The -e setting shall be ignored when executing the compound list following the while, until, if, or elif reserved word, a pipeline
beginning with the ! reserved word, or any command of an AND-OR list
other than the last.
http://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/V3_chap02.html#set
Bash's manual also points this out, with somewhat different wording.
I think that entry in BashFAQ doesn't even try to spell out the specifics, the phrase "These rules are extremely convoluted" makes that pretty clear. Though the linked pages seem to have more than one would want to know.
Because that's what the standard says:
-e
When this option is on, when any command fails, the shell immediately shall exit, [...], with the following exceptions:
- The -e setting shall be ignored when executing the compound list following the while, until, if, or elif reserved word, a pipeline
beginning with the ! reserved word, or any command of an AND-OR list
other than the last.
http://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/V3_chap02.html#set
Bash's manual also points this out, with somewhat different wording.
I think that entry in BashFAQ doesn't even try to spell out the specifics, the phrase "These rules are extremely convoluted" makes that pretty clear. Though the linked pages seem to have more than one would want to know.
edited 1 hour ago
answered 1 hour ago
ilkkachuilkkachu
57k785158
57k785158
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496316%2fwhy-does-operator-with-set-e-always-succeed-even-for-truthy-return-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown