{"id":38672,"date":"2024-03-17T02:35:23","date_gmt":"2024-03-17T02:35:23","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=38672"},"modified":"2024-04-01T03:52:36","modified_gmt":"2024-04-01T03:52:36","slug":"bash-function-return-array","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/","title":{"rendered":"How to Return Array From Bash Function? [7 Methods]"},"content":{"rendered":"<p style=\"text-align: justify;\">To return an array from the function in Bash, use the following methods:<\/p>\n<ol>\n<li>Using Global Variable: <code class=\"\" data-line=\"\">my_function() declare my_array { echo my_array;}<\/code><\/li>\n<li>Using Command Substitution: <code class=\"\" data-line=\"\">variable=$(my_function)<\/code><\/li>\n<li>Using Process Substitution: <code class=\"\" data-line=\"\">variable=$(&lt; &lt;(my_function))<\/code><\/li>\n<li>Using IFS Variable: <code class=\"\" data-line=\"\">IFS=&#039; &#039; variable=$(my_function)<\/code><\/li>\n<li>Using \u201cnameref\u201d Feature: <code class=\"\" data-line=\"\">my_function() {local -n my_array=$1;} declare ref_array<\/code><\/li>\n<li>Using Here-document: <code class=\"\" data-line=\"\">my_function() {cat EOF my_array EOF;}<\/code><\/li>\n<li>Using \u201cprintf\u201d Command: <code class=\"\" data-line=\"\">my_function() {printf &quot;%s\\n&quot; &quot;${my_array[@]}&quot;;}<\/code><\/li>\n<\/ol>\n<p style=\"text-align: justify;\">Bash does not have a built-in way to return an array directly from the function. Nevertheless, declaring a <strong>global variable<\/strong> or using the <strong>command substitution<\/strong> method helps to get an array from the bash function. In this article, I will explore 7 methods to seamlessly return arrays from functions in Bash, assisting you to unleash your scripting potential.<\/p>\n<div class=\"su-button-center\"><a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/Practice_files_bash-function-return-array.zip\" class=\"su-button su-button-style-default\" style=\"color:#000000;background-color:#E8F9FF;border-color:#bac8cc;border-radius:56px\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"color:#000000;padding:8px 26px;font-size:20px;line-height:30px;border-color:#effbff;border-radius:56px;text-shadow:0px 0px 0px #000000\"><i class=\"sui sui-download\" style=\"font-size:20px;color:#1AA1D6\"><\/i> Practice Files to Return Array From Bash Function<\/span><\/a><\/div>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#1_Using_Global_Variable\" >1. Using Global Variable<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#2_Using_Command_Substitution\" >2. Using Command Substitution<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#3_Using_Process_Substitution\" >3. Using Process Substitution<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#4_Using_IFS_Variable\" >4. Using IFS Variable<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#5_Using_%E2%80%9Cnameref%E2%80%9D_Feature\" >5. Using \u201cnameref\u201d Feature<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#6_Using_Here-document\" >6. Using Here-document<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#7_Using_%E2%80%9Cprintf%E2%80%9D_Command\" >7. Using \u201cprintf\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#People_Also_Ask\" >People Also Ask<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#Can_a_Bash_function_return_an_array\" >Can a Bash function return an array?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#How_can_a_function_return_an_array_in_Bash\" >How can a function return an array in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#Can_a_function_return_two_arrays\" >Can a function return two arrays?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#How_to_convert_array_into_string_in_Bash\" >How to convert array into string in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#How_to_return_an_associative_array_from_Bash_function\" >How to return an associative array from Bash function?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"1_Using_Global_Variable\"><\/span>1. Using Global Variable<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">One of the most common approaches to return an array from the function in bash is to define a <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/variables\/scope\/global-variable\/#What_is_a_Global_Variable_in_Bash\" target=\"_blank\" rel=\"noopener\">global variable<\/a><\/u><\/strong> that can be called from both inside and outside of the function. It offers more simplicity, making the script easy to understand. Here\u2019s a Bash script to return an array using a global variable:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\ndeclare -a array\nfunction get_array(){\n  array=(&quot;apple&quot; &quot;banana&quot; &quot;orange&quot; &quot;cherry&quot;)\n}\n\nget_array\necho &quot;${array[0]}&quot;\necho &quot;${array[1]}&quot;\necho &quot;${array[2]}&quot;\necho &quot;${array[3]}&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>Firstly,<code class=\"\" data-line=\"\">declare -a array<\/code> declares a global variable \u201carray\u201d using the <strong><u><a href=\"https:\/\/linuxsimply.com\/declare-command-in-linux\/#Example_3_Use_the_declare_Command_in_Linux_to_Assign_an_Array\" target=\"_blank\" rel=\"noopener\">declare command<\/a><\/u><\/strong> and the <code class=\"\" data-line=\"\">-a<\/code> option indicates that the variable \u201carray\u201d is an <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/array\/index-array\/#What_is_an_Indexed_Array_in_Bash\" target=\"_blank\" rel=\"noopener\">indexed array<\/a><\/u><\/strong>. After that, a function named <code class=\"\" data-line=\"\">get_array<\/code> is defined. Inside the function, <code class=\"\" data-line=\"\">array=(&quot;apple&quot; &quot;banana&quot; &quot;orange&quot; &quot;cherry&quot;)<\/code> assigns the four elements to the variable \u201carray\u201d. When the function is called, <code class=\"\" data-line=\"\">echo &quot;${array[0]}&quot;<\/code> prints the 1st element of the array. Similarly, &#8220;${array[1]}&#8221; &#8220;${array[2]}&#8221; &#8220;${array[3]}&#8221; are used to print 2nd, 3rd, and 4th array element.<\/p>\n<\/div><\/div>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-38675\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/1.returns-array-elements-from-function-using-global-variable.png\" alt=\"returns array elements from function using global variable\" width=\"576\" height=\"192\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/1.returns-array-elements-from-function-using-global-variable.png 576w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/1.returns-array-elements-from-function-using-global-variable-300x100.png 300w\" sizes=\"(max-width: 576px) 100vw, 576px\" \/><\/p>\n<p style=\"text-align: justify;\">The script returns all the array elements from the function using a global variable.<\/p>\n<div class=\"su-note\"  style=\"border-color:#cedfe2;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#e8f9fc;border-color:#ffffff;color:#333333;\">\n<p><strong>Note:<\/strong> You can print the entire array in one line by placing the <code class=\"\" data-line=\"\">echo &quot;${array[@]}&quot;<\/code>\u00a0line after the function call.<\/p>\n<\/div><\/div>\n<h2><span class=\"ez-toc-section\" id=\"2_Using_Command_Substitution\"><\/span>2. Using Command Substitution<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Integrating the command substitution method with the <code class=\"\" data-line=\"\">echo<\/code> command is a straightforward way to catch the output of a function. In this case, the output of the function is an array. Check this script to know how to return an array using the <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/input-output\/output\/echo-command\/\" target=\"_blank\" rel=\"noopener\">echo command<\/a><\/u><\/strong> with the command substitution method:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nfunction return_array(){\n local array=(1 2 3 4 5 6)\n for element in &quot;${array[@]}&quot;; do\n  echo &quot;$element&quot;\n done\n}\n\n# Call the function and capture its output using command substitution\nresult=$(return_array)\n\n# Print the captured output, with each element on a separate line\necho &quot;$result&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>Inside the <code class=\"\" data-line=\"\">return_array<\/code> function, a local variable \u201carray\u201d with values (1 2 3 4 5 6) is declared first. Then a <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/for-loop\/\" target=\"_blank\" rel=\"noopener\">for loop<\/a><\/u><\/strong> iterates over each array element and prints them separately using <code class=\"\" data-line=\"\">echo &quot;$element&quot;<\/code>.\u00a0 After that, <code class=\"\" data-line=\"\">result=$(return_array)<\/code> uses command substitution to capture the function&#8217;s output. Finally, <code class=\"\" data-line=\"\">echo &quot;$result&quot;<\/code> displays the captured output.<\/p>\n<\/div><\/div>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-38676\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/2.returns-array-elements-from-function-using-command-substitution.png\" alt=\"returns array elements from function using command substitution\" width=\"554\" height=\"258\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/2.returns-array-elements-from-function-using-command-substitution.png 554w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/2.returns-array-elements-from-function-using-command-substitution-300x140.png 300w\" sizes=\"(max-width: 554px) 100vw, 554px\" \/><\/p>\n<p style=\"text-align: justify;\">You can see that the function returns each array element using command substitution with the <strong>echo<\/strong> command.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"3_Using_Process_Substitution\"><\/span>3. Using Process Substitution<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">To gracefully handle the function&#8217;s output, consider utilizing the process substitution method with the <code class=\"\" data-line=\"\">echo<\/code> command. This strategy effectively returns the array from a function using the <code class=\"\" data-line=\"\">$(&lt; &lt;(function_name))<\/code> syntax. Here\u2019s how:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nfunction get_array() {\n local array=(&quot;apple&quot; &quot;banana&quot; &quot;orange&quot; &quot;cherry&quot;)\n for element in &quot;${array[@]}&quot;; do\n  echo &quot;$element&quot;\n done\n}\n\nresult=$(&lt; &lt;(get_array))\necho &quot;$result&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>In this script, the <code class=\"\" data-line=\"\">get_array<\/code> function includes an array with 4 elements (&#8220;apple&#8221; &#8220;banana&#8221; &#8220;orange&#8221; &#8220;cherry&#8221;). The <code class=\"\" data-line=\"\">for loop<\/code> iterates over the array elements and shows each array in the terminal using the <code class=\"\" data-line=\"\">echo<\/code> command. Later, <code class=\"\" data-line=\"\">result=$(&lt; &lt;(get_array))<\/code> uses the process substitution approach to catch the function\u2019s output and print it on the console with <code class=\"\" data-line=\"\">echo &quot;$result&quot;<\/code>.<\/p>\n<\/div><\/div>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-38677\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/3.returns-array-elements-from-function-using-process-substitution.png\" alt=\"returns array elements from function using process substitution\" width=\"556\" height=\"191\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/3.returns-array-elements-from-function-using-process-substitution.png 556w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/3.returns-array-elements-from-function-using-process-substitution-300x103.png 300w\" sizes=\"(max-width: 556px) 100vw, 556px\" \/><\/p>\n<p>Returning the array elements from the function smoothly.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"4_Using_IFS_Variable\"><\/span>4. Using IFS Variable<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">IFS stands for <strong>Internal Field Separator<\/strong> which is a special variable in bash used to split the input string into fields based on the character set to an IFS. In this example, the IFS is set to a single space character that will separate the array into individual elements using space. Afterward, the individual array element will be returned with the help of command substitution and <code class=\"\" data-line=\"\">echo<\/code> command.<\/p>\n<p style=\"text-align: justify;\">Check the full bash script to know how the IFS variable works to return an array from the Bash function:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nfunction return_array(){\n local array=(1 2 3 4 5 6)\n echo &quot;${array[@]}&quot;\n}\n\nIFS=&#039; &#039;\narray=$(return_array)\nindividual_elements=($array)\n for element in &quot;${individual_elements[@]}&quot;; do\n  echo &quot;$element&quot;\n done<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>This Bash script first defines a function called <code class=\"\" data-line=\"\">return_array<\/code> that creates a local array variable named \u201carray\u201d containing integers from 1 to 6. <code class=\"\" data-line=\"\">echo &quot;${array[@]}&quot;<\/code> echoes all the array elements separated by spaces.<\/p>\n<p>The script then captures the output of the <code class=\"\" data-line=\"\">return_array<\/code> function into a variable \u201carray\u201d using command substitution. Afterward, <code class=\"\" data-line=\"\">individual_elements=($array)<\/code> splits the \u201carray\u201d into individual elements using the <strong>IFS<\/strong> value.<\/p>\n<p>Finally, it iterates over each element of the &#8220;individual_elements&#8221; array using a <code class=\"\" data-line=\"\">for loop<\/code> and prints each element of the \u201cindividual_elements\u201d array.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-38678\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/4.returns-array-elements-from-function-using-IFS.png\" alt=\"returns array elements from function using IFS\" width=\"508\" height=\"253\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/4.returns-array-elements-from-function-using-IFS.png 508w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/4.returns-array-elements-from-function-using-IFS-300x149.png 300w\" sizes=\"(max-width: 508px) 100vw, 508px\" \/><\/p>\n<p style=\"text-align: justify;\">As you can see, the integer values of the array are printed in the terminal.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"5_Using_%E2%80%9Cnameref%E2%80%9D_Feature\"><\/span>5. Using \u201cnameref\u201d Feature<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Even though Bash cannot directly return arrays from functions, it offers the option to generate a <strong>nameref<\/strong> using the <code class=\"\" data-line=\"\">-n<\/code> parameter to manipulate the array directly. Nameref is a useful feature in bash that allows one to create a reference to a variable with another name, where the reference variable behaves like an alias of the original variable. Changes to the reference variable affect the original variable, and vice versa. Check out the script below to return an array using <code class=\"\" data-line=\"\">nameref<\/code>:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Define function to operate on an array\nfunction get_array {\n local -n array=$1\u00a0 # Create a nameref\n array=(&quot;apple&quot; &quot;banana&quot; &quot;orange&quot; &quot;cherry&quot;)\u00a0 # Use the nameref to assign values\n}\n\n# Declare an array to hold the result\ndeclare -a result_array\n\n# Call the function with the reference to the result_array\nget_array result_array\nfor element in &quot;${result_array[@]}&quot;; do\n echo &quot;$element&quot;\ndone<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>Inside the <code class=\"\" data-line=\"\">get_array<\/code> function, <code class=\"\" data-line=\"\">local -n array=$1<\/code> declares a local variable \u201carray\u201d and the <code class=\"\" data-line=\"\">-n<\/code> option makes it a nameref. This nameref is then assigned the value of the first argument <code class=\"\" data-line=\"\">($1)<\/code> provided to the function. This means that the \u201carray\u201d which contains 4 elements (&#8220;apple&#8221; &#8220;banana&#8221; &#8220;orange&#8221; &#8220;cherry&#8221;) becomes a reference to the array passed to the function.<\/p>\n<p>After that, <code class=\"\" data-line=\"\">declare -a result_array<\/code> declares a &#8220;result_array&#8221; that holds the result returned by the &#8220;get_array&#8221; function. Consequently, <code class=\"\" data-line=\"\">get_array result_array<\/code> calls the get_array function and passes &#8220;result_array&#8221; as an argument, allowing &#8220;get_array&#8221; to modify the &#8220;result_array&#8221; directly via the nameref. Lastly, the <code class=\"\" data-line=\"\">for loop<\/code> iterates over the array elements and prints them in the terminal.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-38680\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/6.returns-array-elements-from-function-using-nameref.png\" alt=\"returns array elements from function using nameref\" width=\"549\" height=\"193\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/6.returns-array-elements-from-function-using-nameref.png 549w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/6.returns-array-elements-from-function-using-nameref-300x105.png 300w\" sizes=\"(max-width: 549px) 100vw, 549px\" \/><\/p>\n<p style=\"text-align: justify;\">Upon executing the script, it shows all the elements of the array.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"6_Using_Here-document\"><\/span>6. Using Here-document<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The here-document passes text into a command or script. To return an array from the function using here-doc, incorporate the <code class=\"\" data-line=\"\">mapfile<\/code> command that converts the text of the here-document into an array. Here\u2019s how:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Function that uses a here-document to return an array\nfunction return_array {\n cat &lt;&lt;EOF\n1\n2\n3\n4\n5\nEOF\n}\n\n# Capture the output in an array using mapfile\nmapfile -t my_array &lt; &lt;(return_array)\n\n# Access the array\necho &quot;Array elements: ${my_array[@]}&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>In this script, the <code class=\"\" data-line=\"\">return_array<\/code> function uses the <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/input-output\/output\/cat-command\/\" target=\"_blank\" rel=\"noopener\">cat command<\/a><\/u><\/strong> to display the content within the delimiter <code class=\"\" data-line=\"\">EOF\u2026EOF<\/code>. Then, <code class=\"\" data-line=\"\">mapfile -t my_array &lt; &lt;(return_array)<\/code> captures the output of the function and converts the text into an array. Here, the <code class=\"\" data-line=\"\">-t<\/code> option removes trailing newline characters from each line read. Lastly, <code class=\"\" data-line=\"\">echo &quot;Array elements: ${my_array[@]}&quot;<\/code> prints out all the elements of the \u201cmy_array\u201d array.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-38679\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/5.returns-array-elements-from-function-using-here-document.png\" alt=\"returns array elements from function using here-document\" width=\"632\" height=\"243\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/5.returns-array-elements-from-function-using-here-document.png 632w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/5.returns-array-elements-from-function-using-here-document-300x115.png 300w\" sizes=\"(max-width: 632px) 100vw, 632px\" \/><\/p>\n<p>You can notice that the script has displayed all the array elements.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"7_Using_%E2%80%9Cprintf%E2%80%9D_Command\"><\/span>7. Using \u201cprintf\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The <code class=\"\" data-line=\"\">printf<\/code> command in Bash prints the text or variables to the standard output. Blending the &#8220;printf&#8221; command with the function provides a compelling way to return an array from the Bash function. See the following script to accomplish this task:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\nget_array() {\n local array=(&quot;apple&quot; &quot;banana&quot; &quot;orange&quot; &quot;cherry&quot;)\n printf &quot;%s\\n&quot; &quot;${array[@]}&quot;\n}\n\nget_array<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>The <code class=\"\" data-line=\"\">get_array<\/code> function contains a local variable \u201carray\u201d which has 4 elements. After calling the function, <code class=\"\" data-line=\"\">printf &quot;%s\\n&quot; &quot;${array[@]}&quot;<\/code> prints all the array elements on a separate line in the terminal.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-38681\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/7.returns-array-elements-from-function-using-printf-command.png\" alt=\"returns array elements from function using printf command\" width=\"550\" height=\"194\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/7.returns-array-elements-from-function-using-printf-command.png 550w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/7.returns-array-elements-from-function-using-printf-command-300x106.png 300w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/p>\n<p style=\"text-align: justify;\">Getting the array from the function using the \u201cprintf\u201d command.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Returning arrays from Bash functions is a daunting task as Bash does not provide any direct ways to do it. But don\u2019t worry, I have shown you 7 methods that can be used to return an array from a function in bash. Choose the method that works best for you. Good luck!<\/p>\n<h2><span class=\"ez-toc-section\" id=\"People_Also_Ask\"><\/span>People Also Ask<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Can_a_Bash_function_return_an_array\"><\/span>Can a Bash function return an array?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Bash function can\u2019t return an array like other programming languages. However, you can simulate the task using a <strong>global<\/strong> or <strong>IFS<\/strong> variable. Moreover, you can apply <strong>command substitution<\/strong> or <strong>nameref<\/strong> feature to achieve this task.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_can_a_function_return_an_array_in_Bash\"><\/span>How can a function return an array in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To return an array from a function in bash, create a function named <code class=\"\" data-line=\"\">my_function<\/code>, then define a global variable <code class=\"\" data-line=\"\">global_var=(1 2 3 4)<\/code> containing all the array elements (1 2 3 4). After that, call the function and use <code class=\"\" data-line=\"\">echo &quot;${global_var[@]}&quot;<\/code> to print the array values in the terminal.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Can_a_function_return_two_arrays\"><\/span>Can a function return two arrays?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\"><strong>Yes<\/strong> definitely, a function can return two arrays. To do so, define two arrays as follows: array1=(red blue green) and array2=(apple banana orange), where \u201carray1\u201d and \u201carray2\u201d are the names of the global variables. After that, print the array elements with <code class=\"\" data-line=\"\">echo &quot;${array1[@]}&quot;<\/code> and <code class=\"\" data-line=\"\">echo &quot;${array2[@]}&quot;<\/code>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_convert_array_into_string_in_Bash\"><\/span>How to convert array into string in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To convert an array into a string, you can use parameter expansion that expands each element of the array into separate words. Suppose, a variable named \u201carray\u201d has 3 elements (\u201cThis\u201d \u201cis\u201d \u201cLinux\u201d), to make them a string \u201cThis is Linux\u201d, use <code class=\"\" data-line=\"\">echo &quot;${array[@]}&quot;<\/code> syntax.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_return_an_associative_array_from_Bash_function\"><\/span>How to return an associative array from Bash function?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">An associative array is a collection of key-value pairs where each key is unique within the array. To return an associative array from the function in bash, check the script below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\n\n# Define a function to populate an associative array\npopulate_array() {\n local -n array_ref=$1\u00a0 # Create a reference to the array passed as argument\n array_ref[&quot;key1&quot;]=&quot;value1&quot;\n array_ref[&quot;key2&quot;]=&quot;value2&quot;\n array_ref[&quot;key3&quot;]=&quot;value3&quot;\n}\n\n# Declare an empty associative array\ndeclare -A my_array\n\n# Call the function to populate the array\npopulate_array my_array\n\n# Access the elements of the array\necho &quot;Key1: ${my_array[&quot;key1&quot;]}&quot;\necho &quot;Key2: ${my_array[&quot;key2&quot;]}&quot;\necho &quot;Key3: ${my_array[&quot;key3&quot;]}&quot;<\/code><\/pre>\n<p><script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [{\n    \"@type\": \"Question\",\n    \"name\": \"Can a Bash function return an array?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Bash function can\u2019t return an array like other programming languages. However, you can simulate the task using a global or IFS variable. Moreover, you can apply command substitution or nameref feature to achieve this task.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How can a function return an array in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To return an array from a function in bash, create a function named my_function, then define a global variable global_var=(1 2 3 4) containing all the array elements (1 2 3 4). After that, call the function and use echo \\\"${global_var[@]}\\\" to print the array values in the terminal.\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"Can a function return two arrays?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"Yes definitely, a function can return two arrays. To do so, define two arrays as follows: array1=(red blue green) and array2=(apple banana orange), where \u201carray1\u201d and \u201carray2\u201d are the names of the global variables. After that, print the array elements with echo \\\"${array1[@]}\\\" and echo \\\"${array2[@]}\\\".\"\n    }\n  },{\n    \"@type\": \"Question\",\n    \"name\": \"How to convert array into string in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"To convert an array into a string, you can use parameter expansion that expands each element of the array into separate words. Suppose, a variable named \u201carray\u201d has 3 elements (\u201cThis\u201d \u201cis\u201d \u201cLinux\u201d), to make them a string \u201cThis is Linux\u201d, use echo \\\"${array[@]}\\\" syntax.\"\n    }\n  }]\n}\n<\/script><\/p>\n<p><span style=\"font-size: 18pt; color: #003366;\"><strong>Related Articles<\/strong><\/span><\/p>\n<ul>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/exit-function\/\" target=\"_blank\" rel=\"noopener\">How to Exit From Function in Bash? [5 Ways]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-0\/\" target=\"_blank\" rel=\"noopener\">How to Return Exit status \u201c0\u201d in Bash [6 Cases]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-string-function\/\" target=\"_blank\" rel=\"noopener\">How to Return String From Bash Function [4 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-boolean-function\/\" target=\"_blank\" rel=\"noopener\">How to Return Boolean From Bash Function [6 Methods]<\/a><\/strong><\/li>\n<\/ul>\n<hr \/>\n<p><strong>&lt;&lt; Go Back to <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Return Values From Bash Function&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1074113,&quot;3&quot;:{&quot;1&quot;:0},&quot;9&quot;:1,&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/\" target=\"_blank\" rel=\"noopener\">Return Values From Bash Function<\/a><\/span> | <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Bash Functions&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1074113,&quot;3&quot;:{&quot;1&quot;:0},&quot;9&quot;:1,&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/\" target=\"_blank\" rel=\"noopener\">Bash Functions<\/a><\/span>\u00a0| <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Bash Scripting Tutorial&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1074113,&quot;3&quot;:{&quot;1&quot;:0},&quot;9&quot;:1,&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/\" target=\"_blank\" rel=\"noopener\">Bash Scripting Tutorial<\/a><\/span><\/strong><\/p>\n\n\n<div class=\"kk-star-ratings kksr-auto kksr-align-center kksr-valign-bottom\"\n    data-payload='{&quot;align&quot;:&quot;center&quot;,&quot;id&quot;:&quot;38672&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\\\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;How to Return Array From Bash Function? [7 Methods]&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 0px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\">\n            <span class=\"kksr-muted\">Rate this post<\/span>\n    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>To return an array from the function in Bash, use the following methods: Using Global Variable: my_function() declare my_array { &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"How to Return Array From Bash Function? [7 Methods]\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#more-38672\" aria-label=\"Read more about How to Return Array From Bash Function? [7 Methods]\">Read more<\/a><\/p>\n","protected":false},"author":314908,"featured_media":38682,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[201],"class_list":["post-38672","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-scripting","tag-return-values","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Return Array From Bash Function? [7 Methods] - LinuxSimply<\/title>\n<meta name=\"description\" content=\"It is difficult to return array directly from the function in Bash. Here, I have explained 7 easy methods to return an array from a function.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Return Array From Bash Function? [7 Methods] - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"It is difficult to return array directly from the function in Bash. Here, I have explained 7 easy methods to return an array from a function.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/\" \/>\n<meta property=\"og:site_name\" content=\"LinuxSimply\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/mitalimostakim.mou\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-17T02:35:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-01T03:52:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/Return-Array-from-function-in-Bash.png\" \/>\n\t<meta property=\"og:image:width\" content=\"400\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Mitu Akter Mou\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mitu Akter Mou\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/functions\\\/return-values\\\/return-function-array\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/functions\\\/return-values\\\/return-function-array\\\/\"},\"author\":{\"name\":\"Mitu Akter Mou\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/54b35370776bb6a79376ea0135412689\"},\"headline\":\"How to Return Array From Bash Function? [7 Methods]\",\"datePublished\":\"2024-03-17T02:35:23+00:00\",\"dateModified\":\"2024-04-01T03:52:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/functions\\\/return-values\\\/return-function-array\\\/\"},\"wordCount\":1507,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/functions\\\/return-values\\\/return-function-array\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/03\\\/Return-Array-from-function-in-Bash.png\",\"keywords\":[\"return values\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/functions\\\/return-values\\\/return-function-array\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/functions\\\/return-values\\\/return-function-array\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/functions\\\/return-values\\\/return-function-array\\\/\",\"name\":\"How to Return Array From Bash Function? [7 Methods] - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/functions\\\/return-values\\\/return-function-array\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/functions\\\/return-values\\\/return-function-array\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/03\\\/Return-Array-from-function-in-Bash.png\",\"datePublished\":\"2024-03-17T02:35:23+00:00\",\"dateModified\":\"2024-04-01T03:52:36+00:00\",\"description\":\"It is difficult to return array directly from the function in Bash. Here, I have explained 7 easy methods to return an array from a function.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/functions\\\/return-values\\\/return-function-array\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/functions\\\/return-values\\\/return-function-array\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/functions\\\/return-values\\\/return-function-array\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/03\\\/Return-Array-from-function-in-Bash.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/03\\\/Return-Array-from-function-in-Bash.png\",\"width\":400,\"height\":400,\"caption\":\"Return Array from function in Bash\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/functions\\\/return-values\\\/return-function-array\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Bash Scripting Tutorial\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Bash Functions\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/functions\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Return Values From Bash Function\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/functions\\\/return-values\\\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"How to Return Array From Bash Function? [7 Methods]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"name\":\"LinuxSimply\",\"description\":\"All About Linux\",\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/linuxsimply.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\",\"name\":\"LinuxSimply\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"width\":355,\"height\":48,\"caption\":\"LinuxSimply\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/54b35370776bb6a79376ea0135412689\",\"name\":\"Mitu Akter Mou\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Mitu-Akter-Mou-II-96x96.png\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Mitu-Akter-Mou-II-96x96.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Mitu-Akter-Mou-II-96x96.png\",\"caption\":\"Mitu Akter Mou\"},\"description\":\"Hello, This is Mitu Akter Mou, currently working as a Linux Content Developer Executive at SOFTEKO for the Linuxsimply project. I hold a bachelor's degree in Biomedical Engineering from Khulna University of Engineering &amp; Technology (KUET). Experiencing new stuff and gathering insights from them seems very happening to me. My goal here is to simplify the life of Linux users by making creative articles, blogs, and video content for all of them. Read Full Bio\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/mitalimostakim.mou\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/mitu-akter-mou-915537198\\\/\"],\"url\":\"https:\\\/\\\/linuxsimply.com\\\/author\\\/mou\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Return Array From Bash Function? [7 Methods] - LinuxSimply","description":"It is difficult to return array directly from the function in Bash. Here, I have explained 7 easy methods to return an array from a function.","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:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/","og_locale":"en_US","og_type":"article","og_title":"How to Return Array From Bash Function? [7 Methods] - LinuxSimply","og_description":"It is difficult to return array directly from the function in Bash. Here, I have explained 7 easy methods to return an array from a function.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/mitalimostakim.mou\/","article_published_time":"2024-03-17T02:35:23+00:00","article_modified_time":"2024-04-01T03:52:36+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/Return-Array-from-function-in-Bash.png","type":"image\/png"}],"author":"Mitu Akter Mou","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Mitu Akter Mou","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/"},"author":{"name":"Mitu Akter Mou","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/54b35370776bb6a79376ea0135412689"},"headline":"How to Return Array From Bash Function? [7 Methods]","datePublished":"2024-03-17T02:35:23+00:00","dateModified":"2024-04-01T03:52:36+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/"},"wordCount":1507,"commentCount":0,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/Return-Array-from-function-in-Bash.png","keywords":["return values"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/","name":"How to Return Array From Bash Function? [7 Methods] - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/Return-Array-from-function-in-Bash.png","datePublished":"2024-03-17T02:35:23+00:00","dateModified":"2024-04-01T03:52:36+00:00","description":"It is difficult to return array directly from the function in Bash. Here, I have explained 7 easy methods to return an array from a function.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/Return-Array-from-function-in-Bash.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/03\/Return-Array-from-function-in-Bash.png","width":400,"height":400,"caption":"Return Array from function in Bash"},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/return-function-array\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/linuxsimply.com\/"},{"@type":"ListItem","position":2,"name":"Bash Scripting Tutorial","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/"},{"@type":"ListItem","position":3,"name":"Bash Functions","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/"},{"@type":"ListItem","position":4,"name":"Return Values From Bash Function","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/functions\/return-values\/"},{"@type":"ListItem","position":5,"name":"How to Return Array From Bash Function? [7 Methods]"}]},{"@type":"WebSite","@id":"https:\/\/linuxsimply.com\/#website","url":"https:\/\/linuxsimply.com\/","name":"LinuxSimply","description":"All About Linux","publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/linuxsimply.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/linuxsimply.com\/#organization","name":"LinuxSimply","url":"https:\/\/linuxsimply.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","width":355,"height":48,"caption":"LinuxSimply"},"image":{"@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/54b35370776bb6a79376ea0135412689","name":"Mitu Akter Mou","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Mitu-Akter-Mou-II-96x96.png","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Mitu-Akter-Mou-II-96x96.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Mitu-Akter-Mou-II-96x96.png","caption":"Mitu Akter Mou"},"description":"Hello, This is Mitu Akter Mou, currently working as a Linux Content Developer Executive at SOFTEKO for the Linuxsimply project. I hold a bachelor's degree in Biomedical Engineering from Khulna University of Engineering &amp; Technology (KUET). Experiencing new stuff and gathering insights from them seems very happening to me. My goal here is to simplify the life of Linux users by making creative articles, blogs, and video content for all of them. Read Full Bio","sameAs":["https:\/\/www.facebook.com\/mitalimostakim.mou\/","https:\/\/www.linkedin.com\/in\/mitu-akter-mou-915537198\/"],"url":"https:\/\/linuxsimply.com\/author\/mou\/"}]}},"_links":{"self":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/38672","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/users\/314908"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/comments?post=38672"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/38672\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/38682"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=38672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=38672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=38672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}