In Bash, the short answer is yes, but there is a catch: Bash treats almost everything as a string.
Unlike languages like Java or C++ that are very strict about types, Bash is "weakly typed." This means you can mix numbers and words in the same array using +=.
Example:
You can do this in your script:
MIXED_ARRAY=()
MIXED_ARRAY+=(1) # Adding a number
MIXED_ARRAY+=("Apple") # Adding a string
The array will now hold 1 and Apple with no problem.
The "Catch":
Even though you can mix them, Bash will essentially treat the number 1 as the character "1".
- If you try to add them mathematically (like 1 + 1), Bash will treat them as numbers.
- If you try to print them, Bash treats them as text.
Summary for your Lab:
- Can you put a name in your
NUMBERSarray? Yes. - Can you put a number in your
STRINGSarray? Yes.
However, it is a best practice to keep them separate (like the Lab instructions suggest) so that your code is easy for others (and you!) to read and understand later.
Give it a try in your arrays.sh file if you want to see it in action!