bash - return array from function and display contents
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Secret Catacombs
--
Chapters
00:00 Bash - Return Array From Function And Display Contents
01:51 Accepted Answer Score 5
02:19 Answer 2 Score 2
04:39 Answer 3 Score 0
04:55 Thank you
--
Full question
https://superuser.com/questions/1001755/...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#linux #bash
#avk47
ACCEPTED ANSWER
Score 5
Already answered here.
You should do a minimal search in google, because this was the first link returned for "bash return array"
Edit:
In bash, functions don't return values. They can return a status (the same as other programs).
So, if you want to return something, you should use global variables that are updated inside your function.
ANSWER 2
Score 2
Discussion
If you have come to the point of wanting to return an array from a function, then you are probably aware that you can only return status codes. Boo! You say. :-)
What can we do with other data in a function that we want to use in another function / context?
- echo
Assuming stdout is set to the terminal, at least you can see the contents of a variable or something.
- Output Redirection: > or >>
Not ideal, but possible. :-) There are probably more things you can do, but let's stop here.
Discussion continued ...
Let us say we think option #1 above sounds promising. What usually happens? Something like this ...
function listToString ()
{
echo "$*"
}
Reference: Your UNIX: The Ultimate Guide, 2nd Edition, p. 387 (last para).
If I call doSomething
, it might, say, send a string message to stdout. That output can be captured in two different ways.
- Backticks `doSomething`
- This thing: $(doSomthing)
If that is true, then you can save something you send to stdout in another context.
var1=`doSomething`
or
var1=$(doSomething)
In summary ....
Convert a list to a string. Echo the string. Capture the echoed string with command substitution (see above). Use read
combined with a here string (<<<
) to convert the string into an array. Use array at your leisure.
File: new_users
fsmith
jdoe
Let's say we wanted to add new users with a function we made called addAccounts
that loops over username arguments. The order of march would be.
- Make file
- Read file
- Convert list to a string
- Convert the string to an array.
The code would be something like this
function listToString ()
{
echo "$*"
}
usersString=$(listToString $(cat new_users))
read -a users <<< $usersString
addAccounts "${users[@]}"
listToString
may not work with all lines of input. Test it on your input.
The last line should resolve to:
addAccounts "fsmith" "jdoe"
Many people will not understand the line ...
read -a users <<< $usersString
... because they have never heard of a here string.
This solution does not pass an array from a function, but it does allow you to convert the output of a function to an array. Some are satisfied with converting a list to a string and calling it a day, but if you truly want to deal with an official bash array, the here sting above will do that for you.
ANSWER 3
Score 0
This trick won't always work (When you have values with whitespaces, or you want to return values that depend on input values), but it usually does the work:
array_returning_func() {
echo "cat dog tree"
}
declare -a arr="$(array_returning_func)"
for obj in ${arr[@]}; do
echo "$obj"
done