1
0
mirror of https://github.com/golang/go synced 2024-11-23 18:10:04 -07:00

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: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Agniva De Sarker 2018-03-28 13:45:26 +05:30 committed by Ian Lance Taylor
parent 8623503fe5
commit 7bf631e1fc
2 changed files with 8 additions and 1 deletions

View File

@ -21,7 +21,12 @@ func Expand(s string, mapping func(string) string) string {
if s[j] == '$' && j+1 < len(s) {
buf = append(buf, s[i:j]...)
name, w := getShellName(s[j+1:])
// If the name is empty, keep the $.
if name == "" {
buf = append(buf, s[j])
} else {
buf = append(buf, mapping(name)...)
}
j += w
i = j + 1
}

View File

@ -49,6 +49,8 @@ var expandTests = []struct {
{"${HOME}", "/usr/gopher"},
{"${H}OME", "(Value of H)OME"},
{"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) {