3

NOTE: this question did not help me.

I'm trying to create a task runner for Atom and I've come to the point of running multi-line shell scripts. But

const child_process = require("child_process");
child_process.exec(
    `rm -rf something
         another
         directory`,
    (error, stdout, stderr) => {
        /*...*/
    },
)

Lead to node to think that another and directory were commands.

ThatXliner
  • 414
  • 5
  • 15

1 Answers1

2

What worked for me is

child_process.exec(
    yourCommand.replace(/\n/g, "\\\n"),
    (error, stdout, stderr) => {
        /*...*/
    }
);

Which will modify all newlines to have a backslash before the newline.


For Windows, it would be very similar but replacing all new lines with ^\n instead of \\\n:

child_process.exec(
    yourCommand.replace(/\n/g, "^\n"),
    (error, stdout, stderr) => {
        /*...*/
    }
);
Tiramonium
  • 557
  • 5
  • 15
ThatXliner
  • 414
  • 5
  • 15
  • 1
    True, does not work on windows, but nicely on unix. Do you have an idea, how to resolve this issue on windows? – scipper Aug 24 '21 at 09:31
  • According to [this question](https://stackoverflow.com/questions/605686/how-to-write-a-multiline-command), it seems to me that you can do `yourCommand.replace((/\n/g, "^\n")` on Windows. – ThatXliner Oct 03 '21 at 16:54