Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable transformation does not work with arrays #1081

Open
PigeonF opened this issue Jul 5, 2024 · 2 comments
Open

Variable transformation does not work with arrays #1081

PigeonF opened this issue Jul 5, 2024 · 2 comments

Comments

@PigeonF
Copy link

PigeonF commented Jul 5, 2024

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.

@theclapp
Copy link
Collaborator

theclapp commented Jul 5, 2024

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.

@PigeonF
Copy link
Author

PigeonF commented Jul 5, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants