{"id":23,"date":"2006-07-20T15:01:56","date_gmt":"2006-07-20T22:01:56","guid":{"rendered":"http:\/\/bclary.com\/blog\/2006\/07\/20\/pipefail-testing-pipeline-exit-codes\/"},"modified":"2011-04-20T20:10:48","modified_gmt":"2011-04-21T03:10:48","slug":"pipefail-testing-pipeline-exit-codes","status":"publish","type":"post","link":"https:\/\/bclary.com\/blog\/2006\/07\/20\/pipefail-testing-pipeline-exit-codes\/","title":{"rendered":"pipefail &#8211; testing pipeline exit codes"},"content":{"rendered":"<h2>The problem<\/h2>\n<p>In <code>bash<\/code>, running test programs which indicate their pass or fail status via an exit code can be problematic when they are run as part of a pipeline in Linux, Mac OS X or Cygwin since the reported exit code of a pipeline is normally the exit code of the last program in the pipeline. If the test program is not the last program in the pipeline, then its exit code will be hidden by the exit code of the last program.<\/p>\n<h2>The solution<\/h2>\n<p><code>bash<\/code> version 3 introduced an option which changes the exit code behavior of pipelines and reports the exit code of the pipeline as the exit code of the last program to return a non-zero exit code. So long as none of the programs following the test program report a non-zero exit code, the pipeline will report its exit code to be that of the test program. To enable this option, simply execute:<\/p>\n<pre>set -o pipefail<\/pre>\n<p>in the shell where the test program will execute.<\/p>\n<h2>Caveat Emptor<\/h2>\n<p>The <code>pipefail<\/code> option was introduced in <code>bash<\/code> version 3 which is supported in current Linux, Mac OS X, Cygwin releases of <code>bash<\/code> but <strong>is not supported by the default bash shipped on Mac OS X 10.4 and earlier.<\/strong>.<\/p>\n<p>To work around this lagging version on Mac OS X 10.4, you can simply download, build and install the latest version of <code>bash<\/code> from <a href=\"http:\/\/ftp.gnu.org\/gnu\/bash\/\">http:\/\/ftp.gnu.org\/gnu\/bash\/<\/a> into your <code>\/usr\/local\/bin<\/code> directory. Then code your shell scripts to use the <em>shebang<\/em> <code>!#\/usr\/local\/bin\/bash<\/code> instead of the normal <code>\/bin\/bash<\/code>. For systems such as Linux or Cygwin which already provide a <code>pipefail<\/code> capable version of <code>bash<\/code>, simply create a hard link from <code>\/usr\/local\/bin\/bash<\/code> to the system version \/bin\/bash.<\/p>\n<pre>ln \/bin\/bash \/usr\/local\/bin\/bash<\/pre>\n<p>Updated April 20, 2011 to show Mac OS X 10.5 and later support pipefail. Hat tip to Ben Denckla for the updated Mac OS X support.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The problem In bash, running test programs which indicate their pass or fail status via an exit code can be problematic when they are run as part of a pipeline in Linux, Mac OS X or Cygwin since the reported exit code of a pipeline is normally the exit code of the last program in &hellip; <a href=\"https:\/\/bclary.com\/blog\/2006\/07\/20\/pipefail-testing-pipeline-exit-codes\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;pipefail &#8211; testing pipeline exit codes&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,4],"tags":[],"class_list":["post-23","post","type-post","status-publish","format-standard","hentry","category-mozilla","category-testing"],"_links":{"self":[{"href":"https:\/\/bclary.com\/blog\/wp-json\/wp\/v2\/posts\/23","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bclary.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bclary.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bclary.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bclary.com\/blog\/wp-json\/wp\/v2\/comments?post=23"}],"version-history":[{"count":2,"href":"https:\/\/bclary.com\/blog\/wp-json\/wp\/v2\/posts\/23\/revisions"}],"predecessor-version":[{"id":114,"href":"https:\/\/bclary.com\/blog\/wp-json\/wp\/v2\/posts\/23\/revisions\/114"}],"wp:attachment":[{"href":"https:\/\/bclary.com\/blog\/wp-json\/wp\/v2\/media?parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bclary.com\/blog\/wp-json\/wp\/v2\/categories?post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bclary.com\/blog\/wp-json\/wp\/v2\/tags?post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}