Bash Error: Must Use Subscript When Assigning Associative Array

  • HOME
  • >
  • BASH
  • >
  • Bash Error: Must Use Subscript When Assigning Associative Array
Last Updated: 

In our previous post, Iterate and Check if a Bash Array contains a value, we covered the different type of Bash Arrays available since Bash version >4. When using Associative Arrays, you may improperly declare your Array and get the error must use subscript when assigning associative array. The documentation mention clearly the requirement for the subscript part of the declaration.

Arrays are assigned to using compound assignments of the form name=(value1 ... valuen), where each value is of the form [subscript]=string. [...] When assigning to an associative array, the subscript is required.

In short, when using compound assignments, make sure your script properly define the subscript for each key/value pair, the key(subscript) must be surrounded by square brackets [] and the compound assignment must be properly surrounded by parentheses ().

Mistakes as shown below can easily happen if you try to declare programmatically an array while feeding invalid or incomplete data.

# Error
# Missing square brackets [] arround the subscript
[me@host ~]$ declare -A m myAssociativeArray=(a=123)
bash: myAssociativeArray: a=123: must use subscript when assigning associative array

# Error
# Second assignment is missing square brackets [] arround the subscript
[me@host ~]$ declare -A m myAssociativeArray=([a]=123 b=456)
bash: myAssociativeArray: b=456: must use subscript when assigning associative array

# Error
# First assignment is missing square brackets [] arround the subscript
# Second assignment is missing the subscript
[me@host ~]$ declare -A m myAssociativeArray=(a=123 456)
bash: myAssociativeArray: a=123: must use subscript when assigning associative array
bash: myAssociativeArray: 456: must use subscript when assigning associative array

The proper way to declare a Bash Associative Array must include the subscript as seen below.

# Works
[me@host ~]$ declare -A myAssociativeArray
# myAssociativeArray[subscript]=value
[me@host ~]$ myAssociativeArray[a]=123
[me@host ~]$ myAssociativeArray[b]=456
[me@host ~]$ echo ${myAssociativeArray[*]}
456 123

# Works
[me@host ~]$ declare -A myAssociativeArray=([a]=123 [b]=456)
# declare -A myAssociativeArray=([subscript]=value ...)
[me@host ~]$ echo ${myAssociativeArray[*]}
456 123
Related bash posts that you may like
What is the Bash Null Command?
Learn about the Bash null command, also known as the POSIX shell colon command. This post cover concrete use cases and pitfalls to avoid.
How To Format Date and Time in Linux, macOS, and Bash?
Find out how to manipulate date and time on linux and macOS systems as well as natively in the Bash shell. This post covers all you need to know to format a date from your shell.
How To Use Option as Meta Key in macOS Terminal?
The Meta Key is a modifier key that can be quite helpful to improve your productivity while working in a terminal and bash. This post cover how to enable from the command line the Meta Key in macOS Terminal.