I've been trying to build a small bash script to override some configuration in Node. I'm using zsh, so there's a warning if you redirect output in an existing file (see here). I've been trying the proposed answer, although the output is quite unusual. I was wondering maybe I'm doing something wrong here.
echo 'module.exports = { "foo": 1 }' > foo.js
node -e '
const foo = require("./foo")
foo.bar = 1
console.log(`module.exports = ${JSON.stringify(foo)}`)
'
# output is: module.exports = {"foo":1,"bar":1}
node -e '
const foo = require("./foo")
foo.bar = 1
console.log(`module.exports = ${JSON.stringify(foo)}`)
' > bar.js
cat bar.js
# output is: module.exports = {"foo":1,"bar":1}
node -e '
const foo = require("./foo")
foo.bar = 1
console.log(`module.exports = ${JSON.stringify(foo)}`)
' >| foo.js
# output is: module.exports = {"bar":1}
I was under the impression that >|
would bypass the "overwrite file" warning from zsh, but it does compute a "different than expected" return value.
Actual: module.exports = {"bar":1}
Expected: module.exports = {"foo":1,"bar":1}
Where have gone the bytes of "foo:1"
? Could somebody explain me what's happening?
bash
orzsh
(you mention both)? Note that redirections are carried out before the command is executed. If the redirection truncates the file, it is truncated when the command runs.