Skip to content

Instantly share code, notes, and snippets.

@tanakahisateru
Created August 26, 2014 11:15
Show Gist options
  • Save tanakahisateru/3b768a364021fe13ea71 to your computer and use it in GitHub Desktop.
Save tanakahisateru/3b768a364021fe13ea71 to your computer and use it in GitHub Desktop.
Shell command escape by CoffeeScript
# replacement of node-shell-quote
# quote ['echo', '"Oh"', '>_<;'] # ==> echo '"Oh"' \>_\<\;
quote = (xs)->
(for s in xs
if /["\s]/.test(s) and !/'/.test(s)
"'" + s.replace(/(['\\])/g, '\\$1') + "'"
else if /["'\s]/.test(s)
'"' + s.replace(/(["\\$`(){}!#&*|])/g, '\\$1') + '"'
else
String(s).replace(/([\\$`(){}!#&*|<>;])/g, '\\$1')
).join ' '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment