`docker run` output to bash variable - strange behaviour `docker run` output to bash variable - strange behaviour docker docker

`docker run` output to bash variable - strange behaviour


you need to modify the docker run --rm -ti ubuntu pwd 2>&1 with docker run --rm ubuntu pwd 2>&1probabilly the interactive and tty mode doesn't work fine in a shell variables


Dockers -t option allocates a pseudo terminal for the process to output to. A TTY uses a CRLF for a line ending unlike the usual LF in unix.

The " xyz" in your example output is overwriting the rest of the text from the start of the line due to the carriage return stored in the variable.

The od utility can dump the hex or octal values.

$ docker run -t busybox pwd | od -b0000000   057 015 012                                                    0000003

057 = / 015 = CR 012 = LF

Then the normal output.

$ docker run busybox pwd | od -b0000000   057 012                                                        0000002

Remove the -t and possibly check for errors rather than redirecting stderr to stdout. -i is not required unless the process requires stdin.

PWD=$(docker run --rm ubuntu pwd)[ "$?" == "0" ] || exit 1echo "[$PWD]"


The characters \r\n are present in the output:

$ docker run --rm -ti ubuntu pwd 2>&1 > /tmp/docker.out$ cat -A /tmp/docker.out /^M$$ python -c "import sys; print repr(sys.stdin.read())" < /tmp/docker.out '/\r\n'

Also, don't rely on the output for docker run, because if the ubuntu image is not already present, it will also be pulled and the messages of "Pulling image" will be part of the output.