Shell – splitting a single row command to two parts so it would be more “structured” aesthetically

shell

I have the following command which I run from a script. In the script file it is written in this somewhat long row:

sudo zip -r /var/www/html/html-$(date +\%F-\%T-).zip /var/www/html -x /var/www/html/wp-content/cache

So far so good, but I want to split this command into a few pieces horizontally, like:

sudo zip -r /var/www/html/html-$(date +\%F-\%T-).zip /var/www/html || -x /var/www/html/wp-content/cache

Where || should come non-executed characters that will use just for "aeshtetic" splitting of the command to two parts.

Or maybe even vertically like:

sudo zip -r /var/www/html/html-$(date +\%F-\%T-).zip /var/www/html 
-x /var/www/html/wp-content/cache

What will you say is the best way to achieve that?

Best Answer

If I correctly understand what you're trying to achieve, you should use \. This allows splitting long commands into multiple lines.

sudo zip -r \
    /var/www/html/html-$(date +\%F-\%T-).zip \
    /var/www/html \
    -x /var/www/html/wp-content/cache

Keep in mind that spaces before slashes are important. Shells don't insert them automatically, so command like

echo\
"asdf"

will likely result in a "command not found" (@ilkkachu).

Related Question