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

36,906

Solution 1

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

Solution 2

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"
Share:
36,906

Related videos on Youtube

DASKAjA
Author by

DASKAjA

Updated on September 18, 2022

Comments

  • DASKAjA
    DASKAjA over 1 year

    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.

    • Rich Homolka
      Rich Homolka almost 10 years
      Have you tried with single quotes? Since you don't have any variables to interpolate, you can get away with single quotes here.
    • mpy
      mpy almost 10 years
      A blind shot: If the -e parameter gets executed by another shell instance, you might have to escape twice, so try \\\& and \\\!.
    • Simon Schnell
      Simon Schnell almost 5 years
      @mpy the \\\ trick did it for me, thats the best!