Jump to content

Bash array

Auser

declare -a my_array

my_array=()

format() {

      my_array+=("element")

}

export -f format

for x in 1 2 3 4 5 6 7 8 9; do

     format

done

echo ${my_array[*]}

Why is there no elements on list when I try and print them out with echo. Should there be 9x "element" ? 

Link to comment
Share on other sites

Link to post
Share on other sites

unset my_array
declare -a my_array

my_array=""

format () {

      my_array+=("element $1")

}

export -f format

for x in 1 2 3 4 5 6 7 8 9; do

     format $x

done

echo ${my_array[*]}

 

Can Anybody Link A Virtual Machine while I go download some RAM?

 

Link to comment
Share on other sites

Link to post
Share on other sites

15 hours ago, unijab said:

unset my_array
declare -a my_array

my_array=""

format () {

      my_array+=("element $1")

}

export -f format

for x in 1 2 3 4 5 6 7 8 9; do

     format $x

done

echo ${my_array[*]}

 

Can I use this command aswell?

find / -type d -exec bash -c 'format "$0"' {} \;

will this store results of find in array?

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, Auser said:

Can I use this command aswell?

find / -type d -exec bash -c 'format "$0"' {} \;

will this store results of find in array?

 

pretty sure you'll have to pass each item/line to it at a time

Can Anybody Link A Virtual Machine while I go download some RAM?

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×