I have a docker compose service defined with an environment variable:
command: /run.sh
entrypoint: /bin/sh
environment:
- TEST=*
In run.sh I have the following echo statement:
echo TEST: ${TEST}
The unexpected result of that echo statement is an ls of the root folder inside the docker container.
TEST: bin dev etc home lib media mnt opt proc root run run.sh sbin secrets srv static sys tmp usr var
My goal is to set the environment variable to the literal value *.
I’ve tried single quoting it and escaping it:
environment:
- TEST='*'
environment:
- TEST=\*
In those two cases I get the literal values '*' and \* passed in, both of which break the app that I’m using because it’s expecting the value to be a single * character, not quoted or escaped.
GPT4 considered this unexpected behavior and I’m not finding much on it in google searches.
>Solution :
The issue is unrelated to docker-compose.
Unquoted variable expansion in the shell undergoes word splitting and filename expansion. * is filename expansion trigger in shell. * is replaced by the list of files in the current working directory in the shell script you are running with /bin/sh.
Quote variable expansion to prevent filename expansion and word splitting expansion. Check your scripts with shellcheck.
echo TEST: "$TEST"