The issue was resolved by changing how the script was executed. Previously, it was run directly through Steam. Now, the script is written to a temporary file, made executable, and then executed with sudo bash. This change, using the following code, fixed the problem:
const command = `
cat > /tmp/temp_script.sh << 'EOFSCRIPT'
${escapedScript}
EOFSCRIPT
chmod +x /tmp/temp_script.sh
sudo bash /tmp/temp_script.sh
rm /tmp/temp_script.sh
`;
solved the problem.