mage icon indicating copy to clipboard operation
mage copied to clipboard

sh.RunV removing dollar sign character from strings

Open timcrider opened this issue 5 years ago • 2 comments

given: sh.RunV(command, action, "--user", "foo", "--password", "bar$baz")

The password will always be converted to: barbaz.

I've tried to escape: \$, $$, 'bar$baz' I've tried byte buffers and string slices.

RunV always eats the dollar sign even when it's not meant to be a variable replacement.

timcrider avatar May 15 '19 17:05 timcrider

This is absolutely a problem. There's some designs about how to turn this on or off. I'm sorry for the problems it's causing you. I'll try to come up with a good fix for this.

natefinch avatar May 16 '19 00:05 natefinch

I hit this problem today, and here's the hack I came up with to get around it after studying the os package's implementation.

The os package treats $ followed by any special character ('*', '#', '$', '@', '!', '?', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9') as the mapping of that special character. So $$ will be replaced by the mapping of $ which can be itself.

Replace:

err := sh.RunV(cmd, args...)

With:

env := map[string]string{"$": "$"}  // Escape hack: "$$FOO" becomes "$FOO"
_, err := sh.Exec(env, os.Stdout, os.Stderr, cmd, args...)

If you wanted to support this style of escaping ($$ => $), it would be a simple change to the mapping function in Exec... and maybe you'd want to do the same thing in the target package.

abursavich avatar Aug 06 '19 21:08 abursavich