How to escape "!" and "&" in docker's environment varibles

I have a docker container I want to run and hand it over some passwords. One with an exclamation mark ! and the other one with an ampersand &. So I want to run this:

docker run -i -t --rm \
-e "LDAP_FILTER=(&(objectCategory=person)(objectClass=user)" \
-e "LDAP_PASS=Secret!Password" \
user-prefix/container-name

That does not work. & gets replaced to {LDAP_FILTER} and the ! get truncated. I'm pretty sure I have to escape these. But \! and \& didn't work out.


I found a solution:

  • Using single-quotes instead of double-quotes
  • Escape the ampersand & to \&
  • Not escaping the exclamation mark !

so this works:

docker run -i -t --rm \
-e 'LDAP_FILTER=(\&(objectCategory=person)(objectClass=user)' \
-e 'LDAP_PASS=Secret!Password' \
user-prefix/container-name

If you cannot use single-quotes for the entire string for some reason (e.g. variable expansion), this is probably the best solution: In bash, how do I escape an exclamation mark?

In summary: Use "'!'" around the exclamation mark:

echo "hello there"'!'" and goodbye"