@@ -642,6 +642,18 @@ func lookup(key string, required bool, defaultValue string, l Lookuper) (string,
642642 }
643643
644644 if defaultValue != "" {
645+ // Handle escaped "$" by replacing the value with a character that is
646+ // invalid to have in an environment variable. A more perfect solution
647+ // would be to re-implement os.Expand to handle this case, but that's been
648+ // proposed and rejected in the stdlib. Additionally, the function is
649+ // dependent on other private functions in the [os] package, so
650+ // duplicating it is toilsome.
651+ //
652+ // While admittidly a hack, replacing the escaped values with invalid
653+ // characters (and then replacing later), is a reasonable solution.
654+ defaultValue = strings .ReplaceAll (defaultValue , "\\ \\ " , "\u0000 " )
655+ defaultValue = strings .ReplaceAll (defaultValue , "\\ $" , "\u0008 " )
656+
645657 // Expand the default value. This allows for a default value that maps to
646658 // a different environment variable.
647659 val = os .Expand (defaultValue , func (i string ) string {
@@ -657,6 +669,9 @@ func lookup(key string, required bool, defaultValue string, l Lookuper) (string,
657669 return ""
658670 })
659671
672+ val = strings .ReplaceAll (val , "\u0000 " , "\\ " )
673+ val = strings .ReplaceAll (val , "\u0008 " , "$" )
674+
660675 return val , false , true , nil
661676 }
662677 }
0 commit comments