Home > Mobile >  Line wrap long `python -c` command to be < 80-chars
Line wrap long `python -c` command to be < 80-chars

Time:07-24

I have a Python two-liner to set an environment variable, which I run in bash:

ENV_VAR=$(python -c "from some_package import some_long_command; print(some_long_command())")

In bash, one can use \ to line wrap long commands. I am looking for an equivalent within python -c. Is there some way to line wrap this command so it's not so long?

I would like it to fit within 80-char width. Also, I don't want to make this a Python script, I prefer the python -c route.

CodePudding user response:

Use newlines instead of semicolons.

ENV_VAR=$(python -c "

from some_package import some_long_command
print(some_long_command())

")

I added a couple of extra newlines so the python code stands out.

Or, a here-doc (without extra whitespace to show it can get a bit cramped)

ENV_VAR=$(python3 <<'_END_PYTHON'
from some_package import some_long_command
print(some_long_command())
_END_PYTHON
)
  • Related