Bash Error: Must Use Subscript When Assigning Associative Array

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

In my tutorial, A Complete Guide On How To Use Bash Arrays, I 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 bash error must use subscript when assigning associative array. The documentation mention clearly the requirement for the subscript part of the declaration. The subscript part is sometime called a key or index in other programming languages.

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 a compound assignment like declare -A array_name=([key1]=value1, [key2]=value2), make sure that your shell script properly define the subscript for each key/value pair. The subscript part (key) must be enclosed in 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 [] around the subscript
[me@host ~]$ declare -A myAssociativeArray=(a=123)
bash: myAssociativeArray: a=123: must use subscript when assigning associative array

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

# Error
# First assignment is missing square brackets [] around the subscript
# Second assignment is missing the subscript
[me@host ~]$ declare -A 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...
5 Simple Steps On How To Debug a Bash Shell Script
Learn how to quickly debug scripts in Bash with those 5 simple steps that use some of the Bash shell special properties.
A Complete Guide to the Bash Environment Variables
A complete guide on the Linux Bash environment variables with details on how to set, unset, and use the specials shell variables or define custom environment variables.
How To Make A Custom Bash Shell Prompt
Learn how to customize your shell prompt and the specificity of the Bash Prompt variables PROMPT_COMMAND, PROMPT_DIRTRIM, PS0, PS1, PS2, PS3, and PS4.
The Most Useful Bash Shortcuts That You Will Want To Use
A detailed review of the most useful Terminal and Bash shortcuts to improve your productivity when typing command-lines and how to customize the ones you don't like.