Skip to main content
15 events
when toggle format what by license comment
Apr 19, 2023 at 6:57 comment added mgutt Yep, I disabled pipefail in the meantime. It's much easier to follow the principle "Does the most right pipe cmd do what I need?" instead of "Could there be a race condition anywhere in the pipe?".
Apr 18, 2023 at 19:18 comment added Stéphane Chazelas if errexit is also enabled, you'll need /bin/echo ... || true instead of /bin/echo ...; true. There is a misguided recommendation to use set -euo pipefail floating around that many people seem to be following these days even though errexit and pipefail introduce more problems than they fix. Here (set +o pipefail; echo ... | grep ...) would probably be better. But best would be not to set errexit and pipefail globally and do proper error handling and set pipefail only when needed.
Apr 18, 2023 at 18:07 history edited Kamil Maciorowski CC BY-SA 4.0
added 122 characters in body
Apr 18, 2023 at 18:04 comment added Kamil Maciorowski @mgutt OK, the answer now recognizes the problem. echo is a builtin and when "it" gets SIGPIPE, it's really the subshell that gets SIGPIPE and true is never executed.
Apr 18, 2023 at 18:00 history edited Kamil Maciorowski CC BY-SA 4.0
added 122 characters in body
Apr 18, 2023 at 14:49 comment added mgutt @Kamil I tested (echo "$pid_list"; true) | grep -qF "($script_pid)", but it still fails randomly. Instead echo "$pid_list" | grep -qF "($script_pid)" || [[ $? -eq 141 ]] works. I will update my question, if you want to see the full code block.
Apr 18, 2023 at 14:37 comment added Bodo @mgutt If your script depends on the exit code of the pipe in a similar way, then you might get the same problem. If you use head and/or tail in such a pipe, there are often ways to implement this in a different way. As these examples show, enabling pipefail for the whole script is not a good idea. See also mywiki.wooledge.org/BashPitfalls#set_-euo_pipefail
Apr 18, 2023 at 14:22 comment added mgutt @Bodo Thank you. I already read your above comment. But even if I use the binary operator =~ instead, I still could face this problem with other pipes to head or tail.
Apr 18, 2023 at 12:12 comment added Bodo @mgutt Replacing echo ... | grep ... with shell builtins for substring matching would avoid the pipe and the resulting issue related to pipefail.
Apr 18, 2023 at 11:21 comment added mgutt It has been requested for shellcheck: github.com/koalaman/shellcheck/issues/1109
Apr 18, 2023 at 10:44 vote accept mgutt
Apr 18, 2023 at 10:44 comment added mgutt Yes, you are right. I'm using pipefail and then it should be as you explained. Thank you!
Apr 18, 2023 at 9:41 history edited Kamil Maciorowski CC BY-SA 4.0
added 76 characters in body
Apr 18, 2023 at 9:26 history edited Kamil Maciorowski CC BY-SA 4.0
added 155 characters in body
Apr 18, 2023 at 9:21 history answered Kamil Maciorowski CC BY-SA 4.0