If I run /usr/bin/env bash -c "docker info"
in the terminal, I get the appropriate output.
I try to replicate this in Java as below
ProcessBuilder pb = new ProcessBuilder("/usr/bin/env", "bash", "-c", "\"docker info\"");
pb.redirectErrorStream(true);
pb.start();
This fails as bash: docker info: command not found
. I figured that it is treating it as single command and you can work around this by getting rid of those escaped quotes and get it to work. But if you have a command that is not a simple one like docker info
but something like this
/usr/bin/env bash -c "grep docker -m1 /proc/self/cgroup|echo \$(read s;s=\${s##*/};s=\${s#*docker-};s=\${s%.scope};echo \$s)"
where without the quotes the command will not run on the terminal nor in the process builder I have above (for the same reason), but with quotes it works in the terminal but not in the above process builder because it is look for a file or directory of that quoted name.
Well after Charles Duffy extensively worked on this, whose findings are detailed here as well as linked post within that answer, I realized that I don't have to escape embedded commands/scripts/substitutions like I would regularly for the same command executed through a script or a terminal.
The reason is that when executing through process builder, there is no processing done on the command itself (which would be always done by sh
/bash
the terminal was running). I fell into this rabbit hole, because I was making sure the command worked in the terminal first before testing it on Java.