Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 10 down vote accepted

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` # Or $(hostname)

2) Use a shortcut with back ticks (` 's)

That is just the key above Tab on most keyboard (or the Tilde key ~).

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

share|improve this answer
    
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 3: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 3:36
    
Thanks, I really appreciate it! –  Jay LaCroix Nov 21 '12 at 3:38
    
Anytime @JayLaCroix - Welcome to SU! –  nerdwaller Nov 21 '12 at 3:40

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 \
/
share|improve this answer
    
Dang it @Paul! Just beat me :D –  nerdwaller Nov 21 '12 at 3:17
    
@nerdwaller Heh, I thought yours got in first! –  Paul Nov 21 '12 at 3:23
    
I went back to update it to make it more useful and get the indentations. I love SU though, by and large. –  nerdwaller Nov 21 '12 at 3:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.