I am trying to run some Unix commands by using Java on both Windows GitBash and Linux bash (AWS EC2 instance).
Code is here Open git bash using ProcessBuilder and execute command in it
However, the same code works perfectly fine on Linux and fails under Windows GithBash.
This is the command passed to the ProcessBuilder which give trouble
String Message = "Something+here:HH::1234'Description One+Possible spaces'Something else::here'AnotherDescription'Another One:12345'Description'";
String cmd1 = "echo \"" + Message + "\" > " + destinationPath;
I need to include the quotes and spaces in the Message.
What could be the reason of this difference?
In Windows GitBash I get the message partially prompted. Don't get past the first space. File is not created, quote is not displayed:
> Trying to execute command echo "Something+here:HH::1234Description One+Possible spaces Something else::hereAnotherDescription Another One:12345Description" > destinationPath
> Something+here:HH::1234Description
single quotes are not showed.
While in Linux the all Message is prompted and file is created:
> Trying to execute echo "Something+here:HH::1234'Description One+Possible spaces'Something else::here'AnotherDescription'Another One:12345'Description'" > destinationPath
single quotes are showed and file is created.
What can be the reason of this?
ProcessBuilderproperly.echois a shell built-in, so you need to invoke the shell interpreter on each system explicitly. That will probably bebashon Linux andcmdon Windowsechois also an executable. But nonetheless, it might be better to treat it as the built-in one instead, then you have 'symmetry' between the two OSs. You also have a redirect, which is most definitely a shell interpreter thing. I don't know git-bash.exe but I would guess it would behave like bash w.r.t. echo. Make sure you inherit IO inProcessBuilderso you see it print to stdout