builder: fix escaping for ENV variables.

/cc @tiborvass 

closes #8636 
Why you need two same calls? Maybe you should find `idx` first?