Skip to content

Commit

Permalink
Merge pull request blang#3 from JeanMertz/dollar-parenthesis
Browse files Browse the repository at this point in the history
correctly handle `$(` subshells
  • Loading branch information
blang authored Jul 1, 2016
2 parents 9c3c5d7 + 122df52 commit 87362ac
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@ $ expenv -i -f inputfile // Replace inplace
```

Example input:
```bash
```
My PWD is $PWD
Whoami: ${USER}
I'm using $TERM
Expand $empty but don't expand $$empty # => Expand but don't expand $empty
Expand $empty but don't expand $$empty
Don't expand $(echo empty)
```

As mentioned in the above example, two special cases exist:

* `$$` is converted to `$`, allowing you to pass in literal-dollar-signs
* `$(` is not converted, allowing your input to contain subshell like behaviour

Motivation
-----

Expand Down
7 changes: 6 additions & 1 deletion expenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,10 @@ func FileReplaceWriteCloser(f *os.File, replacePath string) io.WriteCloser {
// Follow os.ExpandEnv's contract except for `$$` which is transformed to `$`
func expandEnv(s string) string {
os.Setenv("EXPENV_DOLLAR", "$")
return os.ExpandEnv(strings.Replace(s, "$$", "${EXPENV_DOLLAR}", -1))
os.Setenv("EXPENV_PARENTHESIS", "$(")

s = strings.Replace(s, "$$", "${EXPENV_DOLLAR}", -1)
s = strings.Replace(s, "$(", "${EXPENV_PARENTHESIS}", -1)

return os.ExpandEnv(s)
}

0 comments on commit 87362ac

Please sign in to comment.