"echo" in exec() doesn't execute /bin/echo, but echo command of /bin/sh - confusing
Test 1
When running echo -n -e "\x41" with bash, everything works like expected. Output is A.
But if I use this script in JavaScript adapter (on the same system):
exec(`echo -n -e "\\x41"`, async function (error, result, stderr) {
console.log(result);
});
Output is -e \x41
Why is the option -e added to the output and is not interpreted?!
Test 2
When running /bin/echo -n -e "\x41" with bash, everything works like expected. Output is A.
With this script in JavaScript adapter (on the same system):
exec(`/bin/echo -n -e "\\x41"`, async function (error, result, stderr) {
console.log(result);
});
The output is also A. Whats the difference to Test 1? (which echo returns /bin/echo in all cases)
Versions:
- Adapter version:
- JS-Controller version: 4.0.23
- Node version: v16.17.0
- Operating system: Debian Bullseye
See:
- https://nodejs.org/docs/latest-v16.x/api/child_process.html#child_processexeccommand-options-callback
- https://linux.die.net/man/1/echo
-n
do not output the trailing newline
-e
enable interpretation of backslash escapes
Ok - got it. The problem is, that child_process.exec uses /bin/sh by default. But there is no possibility to override that option:
https://github.com/ioBroker/ioBroker.javascript/blob/d273e8aa90e40da1527bb692a3ded515ccdad065/lib/sandbox.js#L1119
Signature is
child_process.exec(command[, options][, callback])
In sh the echo is a command not a executable
omg 😄
Suggestion
Maybe the instance configuration should contain an option, which shell should be used to execute the shell commands /bin/sh as default and /bin/bash as an alternative option.
Also for security reasons you should always pass the full path to the executable you want to execute. In fact dependeing on the shell there might be the PATH env variable defined differently.
I'm not sure if this should be a "works as expected" :-)
I'm not sure if this should be a "works as expected" :-)
It is working as expected. Using echo with sh was a (confusing) edge case. Giving the users more options would also lead to more support. Lets close this one...
I will update the documentation
And honestly ... we can also allow access to "options"