Search code examples
pythonbashcommand-linecontinuous-integrationword-wrap

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


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.


Solution

  • 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
    )