Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Node.JS Child Process Change Type of Terminal

I’m using the Node JS child process to execute terminal commands and get the output of them, which is all going great. However, I’m on a WSL Windows machine, and I would like to change the type of the child processes terminal to zsh.

Below is my current code for executing a command and receiving the output.

async function execute(
    command: string,
    cwd: string,
): Promise<{ output: string; error: string }> {
    const out = await promisify(exec)(command, { cwd: cwd });

    const output = out.stdout.trim();
    const error = out.stderr.trim();

    return { output: output, error: error };
}

How can I change the type of the terminal from command prompt to zsh?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you can do this

exec(command , { shell : "zsh" })

you can read more about shell requirements here : https://nodejs.org/api/child_process.html#shell-requirements

So your code should look like this :

async function execute(
    command: string,
    cwd: string,
): Promise<{ output: string; error: string }> {
    const out = await promisify(exec)(command, { cwd: cwd , 
    shell:"zsh"});

    const output = out.stdout.trim();
    const error = out.stderr.trim();

    return { output: output, error: error };
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading