Shellcheck: SC2154 fires on associative array indices.

Created on 24 Apr 2015  路  6Comments  路  Source: koalaman/shellcheck

When run on this script

#!/bin/bash

declare -A arr=()

declare -p arr
arr[k1]=lfkej
declare -p arr

# Silence SC2034 warning about arr being unused.
: "${arr[@]}"

shellcheck reports

In arr.sh line 6:
arr[k1]=lfkej
    ^-- SC2154: k1 is referenced but not assigned.

but arr is an associative array and the contents of [] for them are not arithmetically evaluated.

Most helpful comment

Fixed in 07fd5724

All 6 comments

I can verify this. I worked around it by declaring inline:

declare -A array=( [k1]=lfkej )

Nice work-around @pbiggar, I confirm it works, thanks!

I have same problem
2 @pbiggar - inline declare breaks readability in big arrays(

@strizhechenko

but you can improve readability like this:

#color matching criteria (case insensitive)
declare -A colorm=(
    [red]="red"
    [orange]="orange"
    [yellow]="yellow"
    [green]="green"
    [blue]="blue"
    [black]="black"
    [violet]="violet\|purple\|magenta\|pink"
    [all]="all of \(the above\|them\)"
)

Fixed in 07fd5724

Awesome, thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nathaniel112 picture nathaniel112  路  4Comments

balloonpopper picture balloonpopper  路  4Comments

tdmalone picture tdmalone  路  3Comments

hugovk picture hugovk  路  4Comments

arth1 picture arth1  路  4Comments