I am trying to get the "user" and "pw" variables to resolve in a heredoc that is wrapped with single quotes because it is part of a larger command. I believe that as described here Using variables inside a bash heredoc the issue is due to single quotes around the entire command including the "END" portion, but I am not seeing an alternative to wrapping the entire command in quotes because the heredoc is being passed to the mongo shell as a single argument in kubernetes. The code is as follows:
#!/bin/bash
user="theUser"
pw="thePW"
doc='mongo <<END
use DATABASE
db.auth("$user", "$pw")
db.collection.find()
END
'
value=$(kubectl exec -it mongo-pod -- bash -ec "$doc")
echo $value
Put double quotes around the string in the variable assignment, so that variables will be expanded in it.
doc="mongo <<END
use DATABASE
db.auth('$user', '$pw')
db.collection.find()
END
"