Using simple arrays in bash

Posted on
With Bash to explicitly declare an array you have to use
declare -A mioarray
To have the value of element 5 just write
echo $[mioarray[5]
While to change the value you have to write
mioarray[5]='new value'
${mioarray[*]} o ${mioarray[@]} is expanded by all array values while $[#mionome[@]} is expanded with the length of the array What can we do with the arrays? For example, store the output of a command whose line we would like to go to a different item.
IFS=$'\n' mioarray=($(ls -alh))
At this point we can print output as
 IFS=$'\n' echo "${mioarray[*]}"
Attention: Double quotes are important for not having the output on a single line. Doing so we could do several tests on the same captured output once.    

Leave a Reply

Your email address will not be published. Required fields are marked *