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" \

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.

  • 3
    Have you tried with single quotes? Since you don't have any variables to interpolate, you can get away with single quotes here. Jun 30, 2014 at 16:11
  • 1
    A blind shot: If the -e parameter gets executed by another shell instance, you might have to escape twice, so try \\\& and \\\!.
    – mpy
    Jun 30, 2014 at 17:53
  • @mpy the \\\ trick did it for me, thats the best! Aug 1, 2019 at 13:53

2 Answers 2


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' \

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"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .