Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing echo command in ProcessBuilder

Tags:

java

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?


1 Answers

| needs to be interpreted by a shell.

String[] cmd = {"bash", "-c", "echo 'obase=94; 100' | bc"};
like image 159
Reimeus Avatar answered Oct 28 '25 19:10

Reimeus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!