I have the following sh step to execute some commands on Jenkins Pipeline, note the npm install installs bower from package.json for later commands to work
    sh """
    . .env/bin/activate
    . ${JENKINS_HOME}/nvm_init.sh
    npm install
    node --version
    which node
    echo $PATH
    export PATH=\$(npm bin):$PATH
    echo $PATH
    which bower
    which node
    node --version
    bower install
    """
The output starting from npm install as below:
+ npm install
npm WARN [email protected] No repository field.
npm WARN [email protected] No license field.
+ node --version
v6.9.2
+ which node
/data/jenkins/.nvm/versions/node/v6.9.2/bin/node
+ echo /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin
+ npm bin
+ export PATH=/data/jenkins/workspace/GoConnect_feature_jenkins2-KGJYBCCDJPBPOLJ2K2Q4JVQP6TKHIVKXYDJZ4WUMFHTSSDIZWCAQ/node_modules/.bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin
+ echo /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin
+ which bower
/data/jenkins/workspace/GoConnect_feature_jenkins2-KGJYBCCDJPBPOLJ2K2Q4JVQP6TKHIVKXYDJZ4WUMFHTSSDIZWCAQ/node_modules/.bin/bower
+ which node
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Here is a couple of things I don't understand:
- Notice the second 
echo $PATH, it has the same path as previously and didn't contain the previous prepend fromexport PATH=\$(npm bin):$PATH - The last 
which nodedidn't work, why? 
                        
Thanks to colleague @john-carter pointing out that without escaping the the dollar sign, groovy will evaluate before executing the script.