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?
5 Answers
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:
Set HOSTNAME
HOSTNAME=$(hostname)
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
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 \
/ 5 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[@]} 1 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 \ / 1 Axel Heider provided a good alternative to backslashes. Two notes:
- The command can be included in the list, and
- The use of the list should be in double quotes
"${PARAMS[@]}", so that any spaces in parameters get preserved.
#!/bin/bash
params=( show hello, world "multi word"
)
function show { echo --- Showing \""$@"\" as parameters --- for i in "$@"; do echo i=$i done
}
${params[@]}
"${params[@]}"outputs
$ bash test.sh --- Showing "hello, world multi word" as parameters --- i=hello, i=world i=multi i=word --- Showing "hello, world multi word" as parameters --- i=hello, i=world i=multi word