Skip to content

Instantly share code, notes, and snippets.

@horttanainen
Created April 14, 2019 11:52
Show Gist options
  • Save horttanainen/0e79225ef2ed6bbd94a8e3e07637d226 to your computer and use it in GitHub Desktop.
Save horttanainen/0e79225ef2ed6bbd94a8e3e07637d226 to your computer and use it in GitHub Desktop.
execute javascript in shell
#!/usr/bin/env bash
JS='
var input = "";
if (process.argv[1] === "-p") {
process.stdin.resume();
process.stdin.setEncoding("utf8");
process.stdin.on("data", function(buf) {
input += buf;
});
process.stdin.on("end", function() {
eval(process.argv[2]);
});
} else {
eval(process.argv[1]);
}
'
node -e "$JS" -- "$@"
@horttanainen
Copy link
Author

echo "[1, 2, 3, 4, 5]" | js -p "console.log(JSON.parse(input).map(el => el * 2));"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment