Returning array from a Bash function
Solution 1
This works fine as described. The most likely reason it doesn't work in your actual code is because you happen to run it in a subshell:
cat textfile | create_some_array
echo ${a[*]}
would not work, because each element in a pipeline runs in a subshell, and
myvalue=$(create_some_array)
echo ${a[*]}
would not work, since command expansion happens in a subshell.
Solution 2
This won't work as expected when there are whitespaces in the arrays:
function create_some_array() {
local -a a=()
for i in $(seq $1 $2); do
a[i]="$i $[$i*$i]"
done
echo ${a[@]}
}
and worse: if you try to get array indices from the outside "a", it turns out to be a scalar:
echo ${!a[@]}
even assignment as an array wont help, as possible quoting is naturally removed by the echo line and evaluation order cannot be manipulated to escape quoting: try
function create_some_array() {
...
echo "${a[@]}"
}
a=($(create_some_array 0 10))
echo ${!a[@]}
Still, printf seems not to help either:
function create_some_array() {
...
printf " \"%s\"" "${a[@]}"
}
seems to produce correct output on one hand:
$ create_some_array 0 3; echo
"0 0" "1 1" "2 4" "3 9"
but assignment doesn't work on the other:
$ b=($(create_some_array 0 3))
$ echo ${!b[@]}
0 1 2 3 4 5 6 7
So my last trick was to do assignment as follows:
$ eval b=("$(create_some_array 0 3)")
$ echo -e "${!b[@]}\n${b[3]}"
0 1 2 3
3 9
Tataaa!
P.S.: printf "%q " "${a[@]}" also works fine...
Solution 3
You can make an array local to a function, and then return it:
function create_some_array(){
local -a a=()
for i in $(seq $1 $2); do
a[i]=$i
done
echo ${a[@]}
}
declare -a a=()
a=$(create_some_array 0 10)
for i in ${a[@]}; do
echo "i = " $i
done
Solution 4
Hi here is my solution:
show(){
local array=()
array+=("hi")
array+=("everything")
array+=("well?")
echo "${array[@]}"
}
for e in $(show);do
echo $e
done
Try this code on: https://www.tutorialspoint.com/execute_bash_online.php
KayKo
Updated on June 11, 2022Comments
-
KayKo almost 2 years
I am making a bash script and I have encountered a problem. So let's say I got this
function create_some_array(){ for i in 0 1 2 3 .. 10 do a[i]=$i done } create_some_array echo ${a[*]}
Is there any way I can make this work? I have searched quite a lot and nothing I found worked. I think making the
a[]
a global variable should work but I can't find something that actually works in my code. Is there any way to return the array from the function to main program?Thanks in advance
-
KayKo about 11 yearsj=
ls -i "$i" | awk '{print $1}'
; inode[count]=$j; This is the part i insert into an array. And when I try to echo this outside the function it dosnt work -
danday74 almost 3 yearsthis works +1 but its kinda annoying coz if u dump the length of the array it is always 1 - my IDE warns me its converted it to a string and the console output agrees
-
danday74 almost 3 yearsannoying! this works but my IDE is given me an error that it wont suppress ... parsing stopped here. Invalid use of parantheses? .. annoying but +1