Home Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

function return array

Tags
array, function, shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 05-05-2008
function return array

Hi all

I would like to know if there is a way to return an array for a function.
As I know function can return all the contents in an array, I want to return an array type.
# 2  
Old 05-05-2008
What scripting language?
# 3  
Old 05-05-2008
bash shell script
# 4  
Old 05-05-2008
A function can only return a return code, which is a cardinal (positive integer or zero).

However a function can modify an array held in the calling script.

Code:
$ cat function_return_array
#!/bin/bash

function myarray()
{
        a[1]=moja
        a[3]=three
}

a[1]=one
a[2]=two

myarray

echo ${a[1]}
echo ${a[2]}
echo ${a[3]}
$ ./function_return_array
moja
two
three
$

# 5  
Old 05-05-2008
so the array seems act as a globle variable,right?
if a function is called several times in the script, then I need to store it in another array before calling it again, right?
# 6  
Old 05-05-2008
That's correct.
# 7  
Old 05-06-2008
then I have another issue. how to assign an array value to another one i.e. clone?
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Return: can only `return' from a function or sourced script svajhala Shell Programming and Scripting 1 04-05-2018 01:57 PM
Pass array to a function and display the array Girish19 Shell Programming and Scripting 7 02-18-2015 11:26 AM
Return a value from called function to the calling function mvictorvijayan Shell Programming and Scripting 1 09-14-2009 04:19 AM
return in function PRKS Shell Programming and Scripting 5 07-30-2009 08:15 AM
return value of a function fl0r10 Shell Programming and Scripting 5 07-06-2009 02:37 PM
need to return value from function darshakraut Shell Programming and Scripting 5 05-14-2008 08:13 AM
Function call not return value into var katzs500 UNIX for Advanced & Expert Users 4 04-21-2008 01:27 PM
Return an array of strings from user defined function in awk user_prady Shell Programming and Scripting 2 12-03-2007 11:03 PM
Function to return an array of integer dwgi32 Programming 2 11-20-2007 01:08 AM
return value of a function prez Shell Programming and Scripting 3 08-22-2007 05:08 PM


All times are GMT -4. The time now is 11:25 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password