shell quoting in templates, especially for command_line integration
#1784
Unanswered
tianon
asked this question in
Template engine
Replies: 1 comment
-
To be clear, I chose this representation for my example by passing this string through $ python3 -c 'import shlex; print(shlex.quote("foo\n'\''\nbar"))'
'foo
'"'"'
bar'Just to illustrate, here's the same from $ jq -nr '"foo\n'\''\nbaz" | @sh'
'foo
'\''
baz'So there are other ways this could be sliced/represented, but the problem space does have some subtleties that make it worthwhile (IMO) to use a pre-existing library/function for it, especially since there's one already in the Python stdlib. 😄 ❤️ |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the enhancement
While using the
command_lineintegration, I'm creating sensors using shell commands and it strikes me as odd that I can't find an "escape for shell" function/filter in either jinja2 upstream or the HA additions for safely passing arbitrary HA data to shell commands.Example usage
Current workarounds
Mostly hoping and praying (
echo '{{ ... }}').Anything else?
What I'm looking for is something similar to
jq's@shfilter: https://jqlang.org/manual/v1.8/#format-strings-and-escaping:~:text=%2C%20%5C%5C%20respectively.-,%40sh,-%3ASince Python 3.3, the stdlib
shlexclass includes aquotefunction that should do exactly what I'm think of: https://docs.python.org/3.14/library/shlex.html#shlex.quote (big important Windows / non-POSIX shell caveat there which would probably be good to mirror into HA docs)Beta Was this translation helpful? Give feedback.
All reactions