Skip to content

syntax: errors and/or breaks code when arrays have keys that contain pound signs or variables #1285

Closed

Description

@openjck

I'm using shfmt 3.12.0.

The following is valid Bash, and when used in the context of a larger script (like this), it works as expected:

#!/usr/bin/env bash

# ...

for ((I=0; I < ARGS[COMMAND,#]; I++)); do
  ENTR_ARGS+=("${ARGS[COMMAND,$I]}")
done

In the above code, the ARGS array has a key named COMMAND,# and also keys named COMMAND,1, COMMAND,2, etc. It might look a little unusual, but the code is valid, and that pattern is used by docopts.

When shfmt is run on that code, the following error occurs:

ef:4:28: , must be followed by an expression

If that issue is fixed, by quoting the name of the key, like this:

#!/usr/bin/env bash

# ...

for ((I=0; I < ARGS['COMMAND,#']; I++)); do
  ENTR_ARGS+=("${ARGS[COMMAND,$I]}")
done

...and shfmt is run on the file again, then the code is formatted to the following, which does not work:

#!/usr/bin/env bash

# ...

for ((I = 0; I < ARGS['COMMAND,#']; I++)); do
  ENTR_ARGS+=("${ARGS[COMMAND, I]}")
done

In the second-to-last line, the line beginning with ENTR_ARGS+=, notice that a space was inserted after the comma and the dollar sign was removed from the I variable.

As before, the workaround appears to be quoting the name of the key. The nested double quotes look weird, but it works as expected, so I assume it's valid Bash:

#!/usr/bin/env bash

# ...

for ((I = 0; I < ARGS['COMMAND,#']; I++)); do
  ENTR_ARGS+=("${ARGS["COMMAND,$I"]}")
done

Activity

  1. changed the title [-]shfmt errors and/or breaks code when arrays have keys that contain pound signs or variables[/-] [+]syntax: errors and/or breaks code when arrays have keys that contain pound signs or variables[/+] on Mar 15, 2026
  2. added a commit that references this issue on Aug 23, 2026
  3. added a commit that references this issue on Aug 23, 2026
    1599bef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions