Commit 7bf631e1 authored by Agniva De Sarker's avatar Agniva De Sarker Committed by Ian Lance Taylor

os: keep the $ if a variable is not detected

If the character after $ cannot be detected as a valid
variable declaration, do not gobble the $.

Fixes #24345

Change-Id: Iec47be1f2e4f8147b8ceb64c30778eae8045b58f
Reviewed-on: https://go-review.googlesource.com/103055
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 8623503f
...@@ -21,7 +21,12 @@ func Expand(s string, mapping func(string) string) string { ...@@ -21,7 +21,12 @@ func Expand(s string, mapping func(string) string) string {
if s[j] == '$' && j+1 < len(s) { if s[j] == '$' && j+1 < len(s) {
buf = append(buf, s[i:j]...) buf = append(buf, s[i:j]...)
name, w := getShellName(s[j+1:]) name, w := getShellName(s[j+1:])
buf = append(buf, mapping(name)...) // If the name is empty, keep the $.
if name == "" {
buf = append(buf, s[j])
} else {
buf = append(buf, mapping(name)...)
}
j += w j += w
i = j + 1 i = j + 1
} }
......
...@@ -49,6 +49,8 @@ var expandTests = []struct { ...@@ -49,6 +49,8 @@ var expandTests = []struct {
{"${HOME}", "/usr/gopher"}, {"${HOME}", "/usr/gopher"},
{"${H}OME", "(Value of H)OME"}, {"${H}OME", "(Value of H)OME"},
{"A$$$#$1$H$home_1*B", "APIDNARGSARGUMENT1(Value of H)/usr/foo*B"}, {"A$$$#$1$H$home_1*B", "APIDNARGSARGUMENT1(Value of H)/usr/foo*B"},
{"start$+middle$^end$", "start$+middle$^end$"},
{"mixed$|bag$$$", "mixed$|bagPID$"},
} }
func TestExpand(t *testing.T) { func TestExpand(t *testing.T) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment