3 ms·
Yes, you can quote the values and embed them directly, but what if you want to do something like this? shell('echo "Hello, {user}"') You would have to do
by Wingy 2y ago
Yes, you can quote the values and embed them directly, but what if you want to do something like this?
shell('echo "Hello, {user}"')
You would have to do something like:
arg_for_echo = f"Hello, {user}"
shell(f"echo {shlex.quote(arg_for_echo)}")
The main drawback of quoting values like that is that you can't embed them inside an argument in the shell. The environment variable "trick" I used avoids this problem.
Using quotes to sanitize arguments like that is how google/zx (library for writing shell/JS hybrid scripts) handles arguments: https://google.github.io/zx/quotes https://google.github.io/zx/quotes
- oasisaimlessly 2y agoMy proposal: shell('echo '+shlex.quote(f'Hello, {user}')) i.e. never quote any (non-constant?) shell args by hand.