Linux Bash Script, Single Command But Multiple Lines?

117

29

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

Posted 2012-11-21T03:09:10.843

Reputation: 1 191

Answers

153

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

nerdwaller

Posted 2012-11-21T03:09:10.843

Reputation: 13 366

1

It is recommended to use $(command) instead of \command``.

– andrybak – 2015-01-25T10:46:00.080

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  – 2012-11-21T03:26:14.890

If you want your actual "hostname" put it in back ticks (the tilde "~" key above tab): /share/Recovery/Snapshots/\hostname`_$DATE.tar.gz` – nerdwaller – 2012-11-21T03:36:54.943

Anytime @JayLaCroix - Welcome to SU! – nerdwaller – 2012-11-21T03:40:28.270

11

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

Posted 2012-11-21T03:09:10.843

Reputation: 52 173

This doesn't work for me like eg. in alias ub='source ~/.bash_aliases \ && source $HOME/.bash_aliases \ && echo "aliases updated."'; – TheDefinitionist – 2016-10-05T17:12:45.637

1@TheDefinitionist Sounds like a different problem to this one. Perhaps open a new question? – Paul – 2016-10-06T02:58:45.543

Can I line up the backslashes in a column on the right? – SDsolar – 2017-07-30T23:45:15.657

@SDsolar You can use spaces and tabs to line up the slashes. – Paul – 2017-07-31T00:53:50.513

Dang it @Paul! Just beat me :D – nerdwaller – 2012-11-21T03:17:27.577

@nerdwaller Heh, I thought yours got in first! – Paul – 2012-11-21T03:23:21.860

I went back to update it to make it more useful and get the indentations. I love SU though, by and large. – nerdwaller – 2012-11-21T03:35:24.150

6

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

Posted 2012-11-21T03:09:10.843

Reputation: 173