{"id":18653,"date":"2022-08-23T09:01:21","date_gmt":"2022-08-23T07:01:21","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/?p=18653"},"modified":"2022-08-23T09:01:21","modified_gmt":"2022-08-23T07:01:21","slug":"shell-scripting-how-to-return-values-from-functions-in-bash-scripts","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\/","title":{"rendered":"SHELL SCRIPTING: How to return values from functions in bash scripts"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The shell is designed to be a command interpreter. Shell scrips language, if we can call it so, Initially was used to automate&nbsp;simple administrative tasks and flows.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Shell scripting was not designed to give all structural languages facilities,  as <a href=\"https:\/\/www.python.org\/\">Python<\/a> or <a href=\"https:\/\/www.perl.org\/\">Perl<\/a>,  for instance. Nevertheless shell scripting, was and it is intensive used by most of unix and DBA&#8217;s administrators, scripts reaching several hundred lines of code, in many cases spread over several functions and files.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Among others, one of big shell scripting limitation is the return values from functions.<\/p>\n\n\n\n\n\n\n\n<p class=\"wp-block-paragraph\">With no <code>return<\/code> keyword, the function returned value is the last executed return code of the last command executed in the function. This situation cannot be accepted for a production script, as this is totally arbitrary as returned value.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n#!\/bin\/bash\nfunction my_function() {\n    mkdir \/cannot\/create\/this\/directory\n}\n\n# Execute the my_function\nmy_function\nret=$?\necho &quot;The returned value: $ret&quot;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Output  (The returned value is the error code value of the failing <code>mkdir<\/code> command):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nmkdir: cannot create directory \u2018\/cannot\/create\/this\/directory\u2019: No such file or directory\nThe returned value: 1\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Otherwise the return value it the numerical value of the <code>return<\/code> keyword, if it is used. This situation is better than the previous one, but pretty limitative to only numerical values (in fact this is the most used use-case for shell function returns) :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n#!\/bin\/bash\nfunction my_function() {\n    return 99\n}\n\n# Execute the my_function\nmy_function\nret=$?\necho &quot;The returned value: $ret&quot;\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nThe returned value: 99\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">If there is a need to return one or more arbitrary values, like strings or paths, other method should be used: <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n#!\/bin\/bash\nfunction my_function() {\n    local _ret_val1=$1\n    local _ret_val2=$2\n    \n    # ... your code here\n    first_value_to_return=&quot;Hello world&quot;\n    second_value_to_return=&quot;\/path\/to\/nowhere&quot;\n\n    # return values\n    eval $_ret_val1=&quot;&#039;$first_value_to_return&#039;&quot;\n    eval $_ret_val2=&quot;&#039;$second_value_to_return&#039;&quot;\n\n    return 0\n}\n\n# Execute the my_function with parameter1 and parameter2\nmy_function parameter1 parameter2\n\nfunction_return_code=$?\n\necho &quot;First value is: $parameter1&quot;\necho &quot;Second value is: $parameter2&quot;\necho &quot;Function return code is: $function_return_code&quot;\n\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Output: <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nFirst value is: Hello world\nSecond value is: \/path\/to\/nowhere\nFunction return code is: 0\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The advantage of this method is that the numerical returned code can be still be used, by using the <code>return<\/code> keyword.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Despite limitation shell scripting is one of the most used &#8220;languages&#8221; for system admins, DBA&#8217;s. Even if there are big limitation, work arounds, like the one presented, exist for any situation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>BASH: make function to return arbitrary values, not only numbers<\/p>\n","protected":false},"author":27,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","neve_meta_reading_time":"","_themeisle_gutenberg_block_has_review":false,"footnotes":""},"categories":[4133,42],"tags":[1075,965],"class_list":["post-18653","post","type-post","status-publish","format-standard","category-database","category-operating-systems","tag-bash","tag-script"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v28.9 (Yoast SEO v28.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>SHELL SCRIPTING: How to return values from functions in bash scripts - dbi services Blog<\/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:\/\/www.dbi-services.com\/blog\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SHELL SCRIPTING: How to return values from functions in bash scripts\" \/>\n<meta property=\"og:description\" content=\"BASH: make function to return arbitrary values, not only numbers\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi services Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/dbiservices\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-23T07:01:21+00:00\" \/>\n<meta name=\"author\" content=\"Oracle Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@dbiservices\" \/>\n<meta name=\"twitter:site\" content=\"@dbiservices\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Oracle Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\\\/\"},\"author\":{\"name\":\"Oracle Team\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/bf7c7eb0bec165183207237c9d0ee1d5\"},\"headline\":\"SHELL SCRIPTING: How to return values from functions in bash scripts\",\"datePublished\":\"2022-08-23T07:01:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\\\/\"},\"wordCount\":273,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/#organization\"},\"keywords\":[\"Bash\",\"script\"],\"articleSection\":[\"Database\",\"Operating System\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\\\/\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\\\/\",\"name\":\"SHELL SCRIPTING: How to return values from functions in bash scripts - dbi services Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\"},\"datePublished\":\"2022-08-23T07:01:21+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SHELL SCRIPTING: How to return values from functions in bash scripts\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/\",\"name\":\"dbi services Blog\",\"description\":\"#BeSharing\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/#organization\",\"name\":\"dbi services\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/dbi-services-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/dbi-services-logo.png\",\"width\":512,\"height\":512,\"caption\":\"dbi services\"},\"image\":{\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/dbiservices\\\/\",\"https:\\\/\\\/x.com\\\/dbiservices\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/dbi-services\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/dbiservices\"],\"description\":\"Swiss IT consulting and services company specialized in efficient data infrastructures and platforms: databases, application servers, cloud, DevOps, digitalization, analytics, data quality and monitoring. Headquartered in Del\u00e9mont with offices in Nyon, Basel, Bern and Zurich.\",\"email\":\"info@dbi-services.com\",\"telephone\":\"+41 32 422 96 00\",\"legalName\":\"dbi services SA\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/#\\\/schema\\\/person\\\/bf7c7eb0bec165183207237c9d0ee1d5\",\"name\":\"Oracle Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/12\\\/oracle-square.png\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/12\\\/oracle-square.png\",\"contentUrl\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/12\\\/oracle-square.png\",\"caption\":\"Oracle Team\"},\"description\":\"Posts published under the Oracle Team account are collective contributions by the dbi services Oracle consultants. The practice covers the full Oracle Database lifecycle: architecture, migration and upgrade, high availability with RAC and Data Guard, backup and recovery, performance tuning, and engineered systems such as Oracle Database Appliance and Exadata. The team holds Oracle certifications up to Certified Master level and operates customer platforms under service level agreements.\",\"url\":\"https:\\\/\\\/www.dbi-services.com\\\/blog\\\/author\\\/oracle-team\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"SHELL SCRIPTING: How to return values from functions in bash scripts - dbi services Blog","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:\/\/www.dbi-services.com\/blog\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\/","og_locale":"en_US","og_type":"article","og_title":"SHELL SCRIPTING: How to return values from functions in bash scripts","og_description":"BASH: make function to return arbitrary values, not only numbers","og_url":"https:\/\/www.dbi-services.com\/blog\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\/","og_site_name":"dbi services Blog","article_publisher":"https:\/\/www.facebook.com\/dbiservices\/","article_published_time":"2022-08-23T07:01:21+00:00","author":"Oracle Team","twitter_card":"summary_large_image","twitter_creator":"@dbiservices","twitter_site":"@dbiservices","twitter_misc":{"Written by":"Oracle Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\/"},"author":{"name":"Oracle Team","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/bf7c7eb0bec165183207237c9d0ee1d5"},"headline":"SHELL SCRIPTING: How to return values from functions in bash scripts","datePublished":"2022-08-23T07:01:21+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\/"},"wordCount":273,"commentCount":2,"publisher":{"@id":"https:\/\/www.dbi-services.com\/#organization"},"keywords":["Bash","script"],"articleSection":["Database","Operating System"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\/","url":"https:\/\/www.dbi-services.com\/blog\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\/","name":"SHELL SCRIPTING: How to return values from functions in bash scripts - dbi services Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2022-08-23T07:01:21+00:00","breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/shell-scripting-how-to-return-values-from-functions-in-bash-scripts\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"SHELL SCRIPTING: How to return values from functions in bash scripts"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi services Blog","description":"#BeSharing","publisher":{"@id":"https:\/\/www.dbi-services.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.dbi-services.com\/#organization","name":"dbi services","url":"https:\/\/www.dbi-services.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/2026\/09\/dbi-services-logo.png","contentUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/2026\/09\/dbi-services-logo.png","width":512,"height":512,"caption":"dbi services"},"image":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/dbiservices\/","https:\/\/x.com\/dbiservices","https:\/\/www.linkedin.com\/company\/dbi-services\/","https:\/\/www.youtube.com\/user\/dbiservices"],"description":"Swiss IT consulting and services company specialized in efficient data infrastructures and platforms: databases, application servers, cloud, DevOps, digitalization, analytics, data quality and monitoring. Headquartered in Del\u00e9mont with offices in Nyon, Basel, Bern and Zurich.","email":"info@dbi-services.com","telephone":"+41 32 422 96 00","legalName":"dbi services SA"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/bf7c7eb0bec165183207237c9d0ee1d5","name":"Oracle Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/2022\/12\/oracle-square.png","url":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/2022\/12\/oracle-square.png","contentUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/2022\/12\/oracle-square.png","caption":"Oracle Team"},"description":"Posts published under the Oracle Team account are collective contributions by the dbi services Oracle consultants. The practice covers the full Oracle Database lifecycle: architecture, migration and upgrade, high availability with RAC and Data Guard, backup and recovery, performance tuning, and engineered systems such as Oracle Database Appliance and Exadata. The team holds Oracle certifications up to Certified Master level and operates customer platforms under service level agreements.","url":"https:\/\/www.dbi-services.com\/blog\/author\/oracle-team\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/18653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/27"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=18653"}],"version-history":[{"count":0,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/18653\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=18653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=18653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=18653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}