[BUG] Bash associative array with hyphened key malformed.

1
open
alanlunspace
alanlunspace
Posted 4 months ago

[BUG] Bash associative array with hyphened key malformed. #275

Hello,

This bug is serious if it's enabled "format on save" feature.

associative_array=(
  [hyphened-key]=value
   ...
)

would be formatted as

associative_array=(
  [hyphened - key]=value
   ...
)

for shell scripts which breaks 1 single key into 3.

Thanks and regards, Alan

meleu
meleu
Created 1 month ago

This is a known upstream bug caveat.

The shfmt README lists this as a caveat, and suggests:

When indexing Bash associative arrays, always use quotes.

demonstration:

# key surrounded with 'single quotes'
$ echo "array=(['hyphened-key']=value )" | shfmt
array=(['hyphened-key']=value)

# key with "double quotes"
$ echo 'array=(["hyphened-key"]=value )' | shfmt
array=(["hyphened-key"]=value)