{"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-11-28T09:12:28","modified_gmt":"2022-11-28T08:12:28","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>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>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>Among others, one of big shell scripting limitation is the return values from functions.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>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>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>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>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>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>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>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>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":{"_acf_changed":false,"footnotes":""},"categories":[229,42],"tags":[1075,1154],"type_dbi":[],"class_list":["post-18653","post","type-post","status-publish","format-standard","hentry","category-database-administration-monitoring","category-operating-systems","tag-bash","tag-scripts"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.2) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>SHELL SCRIPTING: How to return values from functions in bash scripts - dbi 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 Blog\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-23T07:01:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-28T08:12:28+00:00\" \/>\n<meta name=\"author\" content=\"Oracle Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\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\/66ab87129f2d357f09971bc7936a77ee\"},\"headline\":\"SHELL SCRIPTING: How to return values from functions in bash scripts\",\"datePublished\":\"2022-08-23T07:01:21+00:00\",\"dateModified\":\"2022-11-28T08:12:28+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,\"keywords\":[\"Bash\",\"Scripts\"],\"articleSection\":[\"Database Administration &amp; Monitoring\",\"Operating systems\"],\"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 Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\"},\"datePublished\":\"2022-08-23T07:01:21+00:00\",\"dateModified\":\"2022-11-28T08:12:28+00:00\",\"author\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/66ab87129f2d357f09971bc7936a77ee\"},\"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\":\"Accueil\",\"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 Blog\",\"description\":\"\",\"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\":\"Person\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/66ab87129f2d357f09971bc7936a77ee\",\"name\":\"Oracle Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/f711f7cd2c9b09bf2627133755b569fb5be0694810cfd33033bdd095fedba86d?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f711f7cd2c9b09bf2627133755b569fb5be0694810cfd33033bdd095fedba86d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f711f7cd2c9b09bf2627133755b569fb5be0694810cfd33033bdd095fedba86d?s=96&d=mm&r=g\",\"caption\":\"Oracle Team\"},\"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 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 Blog","article_published_time":"2022-08-23T07:01:21+00:00","article_modified_time":"2022-11-28T08:12:28+00:00","author":"Oracle Team","twitter_card":"summary_large_image","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\/66ab87129f2d357f09971bc7936a77ee"},"headline":"SHELL SCRIPTING: How to return values from functions in bash scripts","datePublished":"2022-08-23T07:01:21+00:00","dateModified":"2022-11-28T08:12:28+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,"keywords":["Bash","Scripts"],"articleSection":["Database Administration &amp; Monitoring","Operating systems"],"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 Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"datePublished":"2022-08-23T07:01:21+00:00","dateModified":"2022-11-28T08:12:28+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/66ab87129f2d357f09971bc7936a77ee"},"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":"Accueil","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 Blog","description":"","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":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/66ab87129f2d357f09971bc7936a77ee","name":"Oracle Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/f711f7cd2c9b09bf2627133755b569fb5be0694810cfd33033bdd095fedba86d?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f711f7cd2c9b09bf2627133755b569fb5be0694810cfd33033bdd095fedba86d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f711f7cd2c9b09bf2627133755b569fb5be0694810cfd33033bdd095fedba86d?s=96&d=mm&r=g","caption":"Oracle Team"},"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":19,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/18653\/revisions"}],"predecessor-version":[{"id":18679,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/18653\/revisions\/18679"}],"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"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=18653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}