{"id":2179,"date":"2023-01-28T08:28:55","date_gmt":"2023-01-28T02:58:55","guid":{"rendered":"https:\/\/trysiteprice.com\/blog\/?p=2179"},"modified":"2023-01-28T08:34:49","modified_gmt":"2023-01-28T03:04:49","slug":"linux-bash-exit-status-and-how-to-set-exit-status-in-bash","status":"publish","type":"post","link":"https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/","title":{"rendered":"Linux bash exit status and how to set exit status in bash"},"content":{"rendered":"<p>In Linux, the exit status of a command or shell script is a numeric value that represents the success or failure of the command or script. The exit status is stored in the special shell variable <code>$?<\/code>. A value of 0 indicates success, and any non-zero value indicates failure.<\/p>\n<p>For example, after running the command <code>ls \/tmp<\/code>, the exit status will be 0 if the \/tmp directory exists and the command was able to list its contents, and non-zero if the \/tmp directory does not exist or the command was not able to list its contents.<\/p>\n<p>You can check the exit status of a command by using the following syntax:<\/p>\n<div class=\"bg-black mb-4 rounded-md\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-bash\"><span class=\"hljs-built_in\">echo<\/span> $?<br \/>\n<\/code><\/div>\n<\/div>\n<p>You can also use the exit status in a shell script to control the flow of execution. For example, you can use an <code>if<\/code> statement to check the exit status and take different actions depending on whether the command succeeded or failed:<\/p>\n<div class=\"bg-black mb-4 rounded-md\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-bash\"><span class=\"hljs-built_in\">command<\/span><br \/>\n<span class=\"hljs-keyword\">if<\/span> [ $? -eq 0 ]; <span class=\"hljs-keyword\">then<\/span><br \/>\n<span class=\"hljs-built_in\">echo<\/span> <span class=\"hljs-string\">\"Command succeeded\"<\/span><br \/>\n<span class=\"hljs-keyword\">else<\/span><br \/>\n<span class=\"hljs-built_in\">echo<\/span> <span class=\"hljs-string\">\"Command failed\"<\/span><br \/>\n<span class=\"hljs-keyword\">fi<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<p>In Bash, you can set the exit status of a script by using the <code>exit<\/code> command followed by the desired exit status. The exit status must be an integer value between 0 and 255. The following example sets the exit status to 0, indicating success:<\/p>\n<div class=\"bg-black mb-4 rounded-md\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-bash\"><span class=\"hljs-built_in\">exit<\/span> 0<br \/>\n<\/code><\/div>\n<\/div>\n<p>And the following example sets the exit status to 1, indicating failure:<\/p>\n<div class=\"bg-black mb-4 rounded-md\">\n<div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans\"><\/div>\n<div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-bash\"><span class=\"hljs-built_in\">exit<\/span> 1<br \/>\n<\/code><\/div>\n<\/div>\n<p>It&#8217;s important to note that the exit status is usually set by the last executed command, and that exit status will be the exit status of the script. So, if you want to set the exit status of the script, it&#8217;s important to do it before any other command that may change the exit status.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Linux, the exit status of a command or shell script is a numeric value that represents the success or failure of the command or script. The exit status is stored in the special shell variable $?. A value of 0 indicates success, and any non-zero value indicates failure. For example, after running the command &#8230; <a title=\"Linux bash exit status and how to set exit status in bash\" class=\"read-more\" href=\"https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/\" aria-label=\"Read more about Linux bash exit status and how to set exit status in bash\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2179","post","type-post","status-publish","format-standard","hentry","category-best-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linux bash exit status and how to set exit status in bash - TrySitePrice<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux bash exit status and how to set exit status in bash - TrySitePrice\" \/>\n<meta property=\"og:description\" content=\"In Linux, the exit status of a command or shell script is a numeric value that represents the success or failure of the command or script. The exit status is stored in the special shell variable $?. A value of 0 indicates success, and any non-zero value indicates failure. For example, after running the command ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/\" \/>\n<meta property=\"og:site_name\" content=\"TrySitePrice\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-28T02:58:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-28T03:04:49+00:00\" \/>\n<meta name=\"author\" content=\"Rahul Sahu\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/\"},\"author\":{\"name\":\"Rahul Sahu\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/#\/schema\/person\/358e04eeea4281deacad2f30c58e67f4\"},\"headline\":\"Linux bash exit status and how to set exit status in bash\",\"datePublished\":\"2023-01-28T02:58:55+00:00\",\"dateModified\":\"2023-01-28T03:04:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/\"},\"wordCount\":265,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/trysiteprice.com\/blog\/#organization\"},\"articleSection\":[\"Best\/Tutorial\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/\",\"url\":\"https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/\",\"name\":\"Linux bash exit status and how to set exit status in bash - TrySitePrice\",\"isPartOf\":{\"@id\":\"https:\/\/trysiteprice.com\/blog\/#website\"},\"datePublished\":\"2023-01-28T02:58:55+00:00\",\"dateModified\":\"2023-01-28T03:04:49+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/trysiteprice.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux bash exit status and how to set exit status in bash\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/#website\",\"url\":\"https:\/\/trysiteprice.com\/blog\/\",\"name\":\"TrySitePrice\",\"description\":\"Free Website Value Calculator Tool\",\"publisher\":{\"@id\":\"https:\/\/trysiteprice.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/trysiteprice.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/#organization\",\"name\":\"TrySitePrice\",\"url\":\"https:\/\/trysiteprice.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/trysiteprice.com\/blog\/wp-content\/uploads\/2021\/12\/cropped-trysiteprice-logo.png\",\"contentUrl\":\"https:\/\/trysiteprice.com\/blog\/wp-content\/uploads\/2021\/12\/cropped-trysiteprice-logo.png\",\"width\":395,\"height\":268,\"caption\":\"TrySitePrice\"},\"image\":{\"@id\":\"https:\/\/trysiteprice.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/#\/schema\/person\/358e04eeea4281deacad2f30c58e67f4\",\"name\":\"Rahul Sahu\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/trysiteprice.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/51f0f95f7b95665f62baed2211572165?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/51f0f95f7b95665f62baed2211572165?s=96&d=mm&r=g\",\"caption\":\"Rahul Sahu\"},\"sameAs\":[\"https:\/\/trysiteprice.com\/blog\"],\"url\":\"https:\/\/trysiteprice.com\/blog\/author\/rsahu4242_trysiteprice\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Linux bash exit status and how to set exit status in bash - TrySitePrice","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/","og_locale":"en_US","og_type":"article","og_title":"Linux bash exit status and how to set exit status in bash - TrySitePrice","og_description":"In Linux, the exit status of a command or shell script is a numeric value that represents the success or failure of the command or script. The exit status is stored in the special shell variable $?. A value of 0 indicates success, and any non-zero value indicates failure. For example, after running the command ... Read more","og_url":"https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/","og_site_name":"TrySitePrice","article_published_time":"2023-01-28T02:58:55+00:00","article_modified_time":"2023-01-28T03:04:49+00:00","author":"Rahul Sahu","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/#article","isPartOf":{"@id":"https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/"},"author":{"name":"Rahul Sahu","@id":"https:\/\/trysiteprice.com\/blog\/#\/schema\/person\/358e04eeea4281deacad2f30c58e67f4"},"headline":"Linux bash exit status and how to set exit status in bash","datePublished":"2023-01-28T02:58:55+00:00","dateModified":"2023-01-28T03:04:49+00:00","mainEntityOfPage":{"@id":"https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/"},"wordCount":265,"commentCount":0,"publisher":{"@id":"https:\/\/trysiteprice.com\/blog\/#organization"},"articleSection":["Best\/Tutorial"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/","url":"https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/","name":"Linux bash exit status and how to set exit status in bash - TrySitePrice","isPartOf":{"@id":"https:\/\/trysiteprice.com\/blog\/#website"},"datePublished":"2023-01-28T02:58:55+00:00","dateModified":"2023-01-28T03:04:49+00:00","breadcrumb":{"@id":"https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/trysiteprice.com\/blog\/linux-bash-exit-status-and-how-to-set-exit-status-in-bash\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/trysiteprice.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Linux bash exit status and how to set exit status in bash"}]},{"@type":"WebSite","@id":"https:\/\/trysiteprice.com\/blog\/#website","url":"https:\/\/trysiteprice.com\/blog\/","name":"TrySitePrice","description":"Free Website Value Calculator Tool","publisher":{"@id":"https:\/\/trysiteprice.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/trysiteprice.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/trysiteprice.com\/blog\/#organization","name":"TrySitePrice","url":"https:\/\/trysiteprice.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/trysiteprice.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/trysiteprice.com\/blog\/wp-content\/uploads\/2021\/12\/cropped-trysiteprice-logo.png","contentUrl":"https:\/\/trysiteprice.com\/blog\/wp-content\/uploads\/2021\/12\/cropped-trysiteprice-logo.png","width":395,"height":268,"caption":"TrySitePrice"},"image":{"@id":"https:\/\/trysiteprice.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/trysiteprice.com\/blog\/#\/schema\/person\/358e04eeea4281deacad2f30c58e67f4","name":"Rahul Sahu","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/trysiteprice.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/51f0f95f7b95665f62baed2211572165?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/51f0f95f7b95665f62baed2211572165?s=96&d=mm&r=g","caption":"Rahul Sahu"},"sameAs":["https:\/\/trysiteprice.com\/blog"],"url":"https:\/\/trysiteprice.com\/blog\/author\/rsahu4242_trysiteprice\/"}]}},"_links":{"self":[{"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/posts\/2179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/comments?post=2179"}],"version-history":[{"count":2,"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/posts\/2179\/revisions"}],"predecessor-version":[{"id":2198,"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/posts\/2179\/revisions\/2198"}],"wp:attachment":[{"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/media?parent=2179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/categories?post=2179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trysiteprice.com\/blog\/wp-json\/wp\/v2\/tags?post=2179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}