Docker execution behavior different in command line and cmd file

I created the following script in cmd file :

docker exec my-container bash -c "stat -c '%U' /my/directory"

I have the following result :

U

But if I do the same in the command line directly, I have this :

root

If I replace U by A (to have rights instead ownership) :

docker exec my-container bash -c "stat -c '%A' /my/directory"

I have that with the command line :

drwxrwxrwx

And that with the cmd script

A

I don’t understand why. For instance, I don’t have any trouble with the following commands :

docker exec my-container bash -c "ls -al /"
docker exec my-container bash -c "su - -c 'ls -al /' myUser"

I have the expected result with the script and the command line.

>Solution :

The underlying problem is windows.
The %U is parsed in a batch file and the percent is removed, because there is no closing percent sign, else it would be replaced by the variable content like in echo %PATH%

On the command line exists different rules for the percent handling, therefore it works here.

To solve it in a batch file, just double the percent signs

docker exec my-container bash -c "stat -c '%%U' /my/directory"

Leave a Reply