Skip to content

Variable transformation does not work with arrays #1081

Closed

Description

@PigeonF

Using "${array[@]@Q}" does not work as expected (I tried some other operators as well and assume this applies to all operators, not just @Q specifically).

$ bash -c 'var="with space"; echo "${var@Q}"'
'with space'
$ : works as expected
$ gosh -c 'var="with space"; echo "${var@Q}"'
'with space'
$ bash -c 'var=("some" "with space"); echo "${var[@]@Q}"'
'some' 'with space'
$ : does not work as expected
$ gosh -c 'var=("some" "with space"); echo "${var[@]@Q}"'
some with space

As you can see, in the last line the result is not quoted.

Activity

  1. theclapp commented on Jul 5, 2024

    @theclapp
    Contributor

    For the unaware (such as myself): The ${parameter@operator} is not in my local bash man page, I had to go looking for it. It came out in bash 4.4, circa late 2015. See https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html and search for ${parameter@operator}.

    The @Q operator:

    The expansion is a string that is the value of parameter quoted in a format that can be reused as input.
    

    There are 9 other operators defined in the GNU Bash documentation. This package handles only @Q and @E, everything else panics. See

    sh/expand/param.go

    Lines 315 to 337 in 793d364

    case syntax.OtherParamOps:
    switch arg {
    case "Q":
    str, err = syntax.Quote(str, syntax.LangBash)
    if err != nil {
    // Is this even possible? If a user runs into this panic,
    // it's most likely a bug we need to fix.
    panic(err)
    }
    case "E":
    tail := str
    var rns []rune
    for tail != "" {
    var rn rune
    rn, _, tail, _ = strconv.UnquoteChar(tail, 0)
    rns = append(rns, rn)
    }
    str = string(rns)
    case "P", "A", "a":
    panic(fmt.Sprintf("unhandled @%s param expansion", arg))
    default:
    panic(fmt.Sprintf("unexpected @%s param expansion", arg))
    }
    .

    Edit: And, obviously, it doesn't work for arrays, regardless.

  2. PigeonF commented on Jul 5, 2024

    @PigeonF
    Author

    Ah, it seems you are right about only Q and E being supported. I remember trying @K as well, but it seems it did not error because I tried it for arrays. The following "succeeds" in the sense that it does not error, but the output is still wrong (which makes sense if expansion operators are just ignored for arrays)

    $ gosh -c 'var=("some" "with space"); echo "${var[@]@K}"'
    some with space
  3. added a commit that references this issue on Nov 3, 2026
    e8f5b9f
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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址