Skip to content

Commit 01c9984

Browse files
committed
expand: don't panic on ${var@U}, ${var@u}, ${var@L}, ${var@K}, ${var@k}
The parser accepts these @-expansion operators, but the expander only handled Q, E, a, A, and P, panicking on the rest. Implement U, u, and L (uppercase-all, uppercase-first, lowercase-all), and treat K and k as unimplemented no-ops so they don't crash either.
1 parent 9c21889 commit 01c9984

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

expand/param.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,18 @@ func (cfg *Config) paramExp(pe *syntax.ParamExp) (string, error) {
337337
}
338338
case "P":
339339
// TODO: implement prompt expansion (\u, \h, \w, etc.).
340+
case "U":
341+
str = strings.ToUpper(str)
342+
case "u":
343+
rs := []rune(str)
344+
if len(rs) > 0 {
345+
rs[0] = unicode.ToUpper(rs[0])
346+
str = string(rs)
347+
}
348+
case "L":
349+
str = strings.ToLower(str)
350+
case "K", "k":
351+
// TODO: implement, like @A but listing keys for assoc arrays.
340352
default:
341353
panic(fmt.Sprintf("unexpected @%s param expansion", arg))
342354
}

interp/interp_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,22 @@ var runTests = []runTest{
735735
`export e=1; echo "${e@A}"`,
736736
"declare -x e=1\n #IGNORE bash always single-quotes",
737737
},
738+
{
739+
`a=Hello; echo "${a@U}"`,
740+
"HELLO\n",
741+
},
742+
{
743+
`a=hello; echo "${a@u}"`,
744+
"Hello\n",
745+
},
746+
{
747+
`a=HELLO; echo "${a@L}"`,
748+
"hello\n",
749+
},
750+
{
751+
`a=foo; echo "<${a@K}><${a@k}>"`,
752+
"<foo><foo>\n #IGNORE not implemented; must not panic",
753+
},
738754
{
739755
"declare a; a+=(b); echo ${a[@]} ${#a[@]}",
740756
"b 1\n",

0 commit comments

Comments
 (0)