Score:0

Understanding the Point of Declaring an Array in Bash

in flag

I'm trying to understand the point of ever explicitly declaring an array in bash. The course I'm doing states that you should create an array by using the command:

$ declare -a ARRAYNAME

I actually never knew about this command because I've never done my arrays like that. As an example, I wrote the script below and then deleted the 1st line of the file. The results were the same. Which begs the question, what's the actual point of ever using this "$ declare -a" statement since my course states that apparently this declare statement must be used (which I proved wrong)?

$ cat test6
declare -a car_models
car_models=( honda toyota bmw )

for i in "${!car_models[@]}"
do
echo " $i ${car_models[$i]}"
done
Esther avatar
es flag
afaik there is no actual difference, you can either create an array with `declare -a array_name` or `array_name=()`.
cn flag
"must be used" is not the same as "you should". and the answer to using it is "readability". This question has nothing to do with Ubuntu; it is a coding behaviour question. seems more like an stackoverflow topic to me. Like: https://stackoverflow.com/questions/19742062/why-are-declare-f-and-declare-a-needed-in-bash-scripts
john smith avatar
in flag
Ok thank you both.
Score:1
cn flag

There is no reason I am aware of why you would have to declare it other than clarity and readibility of your code since declaring it is a clear indication to the next person who reads your code that "Hey, this variable will hold an indexed array".

On the other hand, declaring associative arrays is indeed useful. COnsider this example:

#!/bin/bash
for i in {a..c}; do
  badArray[$i]="foo $i"
done

declare -A goodArray
for i in {a..c}; do
  goodArray[$i]="foo $i"
done

for i in {a..d}; do
  printf 'badArray value for key "%s" is: %s, goodArray value is: %s\n' \
         "$i" "${badArray[$i]}" "${goodArray[$i]}"
done

echo "BAD: ${badArray[@]}"
echo "BAD: ${goodArray[@]}"

Running that gives:

$ foo.sh
badArray value for key "a" is: foo d, goodArray value is: foo a
badArray value for key "b" is: foo d, goodArray value is: foo b
badArray value for key "c" is: foo d, goodArray value is: foo c
badArray value for key "d" is: foo d, goodArray value is: foo d

BAD: foo c
BAD: foo c foo b foo a

As you can see, unlike with regular indexed arrays, if you don't declare the array as associative with declare -A it doesn't work.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.