Skip to content

Instantly share code, notes, and snippets.

@sumerman
Created October 12, 2015 09:36
Show Gist options
  • Save sumerman/b68128e09d1545cea981 to your computer and use it in GitHub Desktop.
Save sumerman/b68128e09d1545cea981 to your computer and use it in GitHub Desktop.
Escape shell arguments in elixir
defmodule Shell do
def escape_value(value), do: escape_value(value, "")
defp escape_value("", res), do: "\"#{res}\""
defp escape_value("\"" <> value, res), do: escape_value(value, res <> "\\\"")
defp escape_value("\\" <> value, res), do: escape_value(value, res <> "\\\\")
defp escape_value(<<char :: utf8, rest :: binary>>, res),
do: escape_value(rest, res <> <<char>>)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment