I am trying to run bash inside a Docker container using docker exec bash -c
and store its output on the host (not the container) by echoing something which will be used in a conditional statement. I could be echoing anything like IP hostname or just a string. The problem is the echo
output:
$ var1=$(docker exec -t test_masternode1 bash -c 'echo 1')
$ echo $var1
1
$ declare -p var1
"eclare -- var1="1
$ var2=1
$ declare -p var2
declare -- var2="1"
$ echo $var2
1
$ if [ "$var1" == "$var2" ];then echo "same";else echo "not same";fi
not same
As you can see var1
and var2
are both 1
but the if
-else
test is failing. Also, the variable declaration output is misaligned. Something is messing up the output from the Docker container; not sure what that is.
I also tried another Docker image from Ubuntu. The example above was from CentOS 7.
$ var3=`docker run -t ubuntu bash -c 'echo 1'`
$ declare -p var3
"eclare -- var3="1
\r
) at the end, as if it had been a line from a DOS text file. I don't know Docker so I can't say whether this is to be expected or not.docker exec -t test_masternode1 bash -c 'echo 1' | sed -e's/\r//'