Reputation:
I've got a script I run like this:
node script.js
From within script.js, I would like to pass a variable back out to the shell for continued use.
I'll say it again in case you didn't get it the first time: basically, running the JS to come up with a variable, then I want to use that variable for a shell command. I tried looking this up and I didn't have much luck figuring it out. Simply doing console.log(foo=bar) doesn't work.
Upvotes: 2
Views: 2268
Reputation: 60097
The shell has to cooperate (well, usually -- you could force it to set the variable through gdb/ptrace, but that's very hacky and you could break the shell). You can output (console.log) the value and the shell can capture it:
variable=$(node script.js)
or you can output, e.g., eval
able text that'll set the value and then eval
it:
eval "$(node -e 'console.log("variable=42")')"
echo $variable
Upvotes: 2