Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running bash script via Node.js - Illegal option -o pipefail

I am trying to exec a bash script via Node.js using child_process.exec(). However it is blowing up on the second line of the file:

#!/usr/bin/env bash
set -eo pipefail; [[ $TRACE ]] && set -x

echo "we are here"

The error returned is:

/bin/sh: 2: set: Illegal option -o pipefail

Why is this happening? When I run the script manually, not from Node it works fine. Here is the Node.js code:

var child = child_proc.exec(bashScript, {
    env: _.extend(process.env, {
        'LB_HOST': config.loadBalancers.lb1
    }),
    timeout: 0
});

child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
like image 635
Justin Avatar asked Jan 20 '26 08:01

Justin


1 Answers

By default when you invoke child_process.exec() it uses /bin/sh which on Ubuntu is actually a symbolic link pointing to /bin/dash. Dash is a stripped down version of bash and I guess does not support:

set -eo pipefail; [[ $TRACE ]] && set -x

Adding the shell option to the Node.js child_proc.exec() fixes this:

shell: '/bin/bash'
like image 165
Justin Avatar answered Jan 22 '26 23:01

Justin



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!