197

I have the following script I wrote by searching Google, and it backs up my Linux system to an archive:

#!/bin/bash
# init

DATE=$(date +20%y%m%d)

tar -cvpzf /share/Recovery/Snapshots/$HOSTNAME_$DATE.tar.gz --exclude=/proc --exclude=/lost+found --exclude=/sys --exclude=/mnt --exclude=/media --exclude=/dev --exclude=/share/Archive /

This works, but I am wondering if I can format the script to show the command over multiple lines, something like this, so it is easy to edit later:

tar -cvpzf /share/Recovery/Snapshots/$HOSTNAME_$DATE.tar.gz 
--exclude=/proc 
--exclude=/lost+found 
--exclude=/sys 
--exclude=/mnt 
--exclude=/media 
--exclude=/dev 
--exclude=/share/Archive 
/

That way it is easier to read and edit later. Is it possible to format a Bash script this way?

Jay LaCroix
  • 1,991
  • 2
  • 12
  • 4

4 Answers4

252

All you should need to do is add "\" at the end of each line and it should be good to go.

So yours will look like:

tar -cvpzf /share/Recovery/Snapshots/$(hostname)_$(date +%Y%m%d).tar.gz \
    --exclude=/proc \
    --exclude=/lost+found \
    --exclude=/sys \
    --exclude=/mnt \
    --exclude=/media \ 
    --exclude=/dev \
    --exclude=/share/Archive \
    /

A Few Shortcuts

(based on your comment update for setting $HOSTNAME)

$HOSTNAME

Two options to set that:

  1. Set HOSTNAME

    HOSTNAME=$(hostname)

  2. Use command substitution (e.g. $(command))

    So it would look like above. That just makes the command run before using it.

$DATE

Another variable avoided would be easily:

$(hostname)_$(date +%Y%m%d).tar.gz \

$ man date will have the formats for the date options, the above is YYYYmmdd

Pablo A
  • 579
  • 5
  • 17
nerdwaller
  • 15,686
  • 2
  • 41
  • 43
  • Thanks guys. One last thing. There seems to be a problem with the file name portion of my script: $HOSTNAME_$DATE.tar.gz When I run the script now, the output file is: 20121120.tar.gz – Jay LaCroix Nov 21 '12 at 03:26
  • If you want your actual "hostname" put it in back ticks (the tilde "~" key above tab): `/share/Recovery/Snapshots/\`hostname\`_$DATE.tar.gz` – nerdwaller Nov 21 '12 at 03:36
  • Anytime @JayLaCroix - Welcome to SU! – nerdwaller Nov 21 '12 at 03:40
  • 2
    It is [recommended](http://stackoverflow.com/a/4708569/1083697) to use `$(command)` instead of `\`command\``. – andrybak Jan 25 '15 at 10:46
  • This works with withspaces as a separator. If I have `ssh` on the first line and commands on the following lines, I think I need a semicolon as well besides backslash. – Timo Nov 22 '20 at 08:23
  • what do you need to do for the last argument do you still need the `\`? – Charlie Parker Dec 01 '21 at 19:40
19

Use the backslash to continue a command on the next line:

tar -cvpzf /share/Recovery/Snapshots/$HOSTNAME_$DATE.tar.gz \
--exclude=/proc \
--exclude=/lost+found \
--exclude=/sys  \
--exclude=/mnt  \
--exclude=/media  \
--exclude=/dev \
--exclude=/share/Archive \
/
Paul
  • 56,727
  • 18
  • 137
  • 162
12

The same command, but with comments for each line, would be:

tar -cvpzf /share/Recovery/Snapshots/$(hostname)_$(date +%Y%m%d).tar.gz `#first comment` \
    --exclude=/proc `#second comment` \
    --exclude=/lost+found `# and so on...` \
    --exclude=/sys \
    --exclude=/mnt \
    --exclude=/media \ 
    --exclude=/dev \
    --exclude=/share/Archive \
    /
Alter Lagos
  • 233
  • 2
  • 7
11

You can use this in bash

PARAMS=(
    -cvpzf /share/Recovery/Snapshots/$HOSTNAME_$DATE.tar.gz 
    --exclude=`enter code here`/proc 
    --exclude=/lost+found 
    --exclude=/sys 
    --exclude=/mnt
    # this is a comment 
    --exclude=/media 
    --exclude=/dev 
    # --exclude=/something
    --exclude=/share/Archive 
    /
)
tar ${PARAMS[@]}
Axel Heider
  • 211
  • 2
  • 3