Bash implementation in Javascript