0

Can someone explain the difference in output when running the following on mac vs linux.

test.sh

#!/bin/bash

testFunc(){
    echo "test function";
}

export -f testFunc;

export testVar="test variable";

package.json

{
    "scripts": {
        "setup": "echo $testVar && testFunc"
    }
}

Command in terminal

source ./test.sh; npm run setup

Output on mac Terminal app running bash

> echo $testVar && testFunc

"test variable"
"test function"

Output on linux WSL ubuntu running bash

> echo $testVar && testFunc

"test variable"
sh: 1: testFunc: not found

----- Edit

You can get around the differences by setting then unsetting the npm default shell.

npm config set script-shell "/bin/bash"; source ./test.sh; npm run setup; npm config delete script-shell

1 Answer 1

2

As you can deduce from the error message, npm-run uses /bin/sh by default, as discussed here.

In Ubuntu, /bin/sh is the Almquist dash shell, which doesn't recognize exported functions. On Mac it's likely bash running in sh compatibility mode - which apparently does.

I don't really know anything about npm, however it should be possible to configure it to use /bin/bash via npm config set script-shell - I don't know enough to know whether that's advisable, or whether you would be better off finding a different way to implement the desired functionality.

1
  • You're correct, dash/bash differences is where my issue lay. Commented Apr 29, 2021 at 10:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .