User Tools

Site Tools

Return to Home page


This shows you the differences between two versions of the page.

Link to this comparison view

cs:bash_language:arrays [2019/12/04 11:39] (current)
zioskenz created
Line 1: Line 1:
 +====== Arrays ======
 +Bash //arrays// and //​associative arrays//.
 +Write an example that illustrates the use of bash //arrays// and //​associative arrays//.\\
 +In bash //array//, the index of the array must be an integer number. Some gaps may be present, i.e., indices can be not continuous.\\
 +Instead, bash //​associative arrays// are like hash maps, and the index of the array must be a string.
 +<code bash>​
 +# Example of use of the BASH arrays (and associative arrays)
 +# Classical array
 +echo "​Classical array"
 +echo ${vett2[3]} ​ # Prints pasta
 +echo ${#​vett2[*]} # Prints 2
 +echo ${vett2[*]} ​ # Prints bread pasta
 +echo ${!vett2[*]} # Prints 3 6
 +# Associative array
 +echo -e "​\nAssociative array"
 +declare -A vett1
 +echo ${vett1[stefano]} ​ # Prints 3
 +echo ${vett1["​giulia"​]} # Prints pippo
 +echo ${#​vett1[*]} ​      # Prints 2
 +echo ${vett1[*]} ​       # Prints pippo 3
 +echo ${!vett1[*]} ​      # Prints giulia stefano
 +for name in ${!vett1[*]}
 +    let sum=${vett1[$name]}+$sum
 +echo "Sum: $sum"
 +unset vett1
 +echo ${#​vett1[*]} # Prints 0 (because vett1 does not exist)
 +<code bash>
 +$ ./
 +Classical array
 +pasta bread
 +3 6
 +Associative array
 +pippo 3
 +giulia stefano
 +Sum: 3

If you found any error, or if you want to partecipate to the editing of this wiki, please contact: admin [at]

You can reuse, distribute or modify the content of this page, but you must cite in any document (or webpage) this url:
/web/htdocs/ ยท Last modified: 2019/12/04 11:39 by zioskenz

Privacy Policy