user5973259 user5973259 - 7 months ago 53
Java Question

Executing echo command in ProcessBuilder

I want to execute

echo 'obase=94; 100' | bc >>out.txt
for that I have used

String[] cmd = {"echo" , "'obase=94; 100'" , " | bc"};
new ProcessBuilder().command(cmd).redirectOutput(ProcessBuilder.Redirect.appendTo(new File("out.txt"))).start();


Inside
out.txt
I see
'obase=94; 100' | bc'
instead of
01 06


Where I am going wrong?

Answer

| needs to be interpreted by a shell.

String[] cmd = {"bash", "-c", "echo 'obase=94; 100' | bc"};