0

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?

4
  • You generally don't need to quote stuff if you're using ProcessBuilder properly. echo is a shell built-in, so you need to invoke the shell interpreter on each system explicitly. That will probably be bash on Linux and cmd on Windows Commented Oct 28, 2022 at 10:37
  • Thanks for comment. So if invoke cmd instead a GitBash it will be fine? In this case is the echo command but Commented Oct 28, 2022 at 12:53
  • I run it with "C:/Program Files/Git/git-bash.exe" under Windows, do I need to use cmd.exe instead? Commented Oct 28, 2022 at 12:59
  • Actually, in Linux, echo is 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 in ProcessBuilder so you see it print to stdout Commented Oct 28, 2022 at 16:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.