I want to have a node script that executes a bash script with an argument that has newline and tab characters.
Node script
const { exec } = require('child_process');
const description = "Incremented values\n\n- android\n\n\t- version: 1.9.4\n\n\t- build: 27\n\n- ios\n\n\t- version: 1.9.4\n\n\t- build: 27";
const childProcess = exec(`sh test.sh "{description}"`);
childProcess.stdout.pipe(process.stdout)
childProcess.stderr.pipe(process.stderr)
childProcess.on('exit', function (exitCode) {
if (exitCode !== 0) {
console.log('\x1b[31m', "Exit code: ", exitCode)
}
else {
console.log("no failures")
}
});
Bash script (test.sh):
DESCRIPTION=$1
generate_create_data() {
cat <<EOF
{
"title": "Release",
"description": "$DESCRIPTION",
"state": "OPEN",
"destination": {
"branch": {
"name": "master"
}
},
"source": {
"branch": {
"name": "release/v1.9.4"
}
}
}
EOF
}
set -x
curl https://fakeurl.com \
-u un:pw \
--request POST \
--header 'Content-Type: application/json' \
-d "$(generate_create_data)"
Unfortunately it looks like while passing the argument from node to the script the newline and tab characters are interpreted
with set -x
you can see this.
...
-d '{
"title": "Release",
"description": "Incremented values
- android
- version: 1.9.4
- build: 27
- ios
- version: 1.9.4
- build: 27",
"state": "OPEN",
...
If I execute the bash script like so
sh test.sh "Incremented values\n\n- android\n\n\t- version: 1.9.4\n\n\t- build: 27\n\n- ios\n\n\t- version: 1.9.4\n\n\t- build: 27"
the newline and tab characters are not interpreted and with set -x
enabled you can see that
-d '{
"title": "Release",
"description": "Incremented values\n\n- android\n\n\t- version: 1.9.4\n\n\t- build: 27\n\n- ios\n\n\t- version: 1.9.4\n\n\t- build: 27",
"state": "OPEN",
...
how do I pass the description variable correctly from node to the bash script so that curl does not try to interpret the newline and tab characters?
Things I've tried
Encoding it as a buffer
const childProcess = exec(`sh src/increment/git/test.sh "${description}"`, {encoding: 'buffer'});
Escaping special characters while passing the argument
const childProcess = exec(`sh src/increment/git/test.sh $'${description}'`);
Base64 encoding
...
const description = "Incremented values\n\n- android\n\n\t- version: 1.9.4\n\n\t- build: 27\n\n- ios\n\n\t- version: 1.9.4\n\n\t- build: 27";
const buff = Buffer.from(description, 'utf-8')
const base64description = buff.toString('base64');
const childProcess = exec(`sh src/increment/git/test.sh "${base64description}"`);
...
B64DESCRIPTION=$1
DESCRIPTION=`echo $B64DESCRIPTION | base64 --decode`
...
This gave me the same result as before