Linux Bash Script, Single Command But Multiple Lines?
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?
linux bash script tar
add a comment |Â
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?
linux bash script tar
add a comment |Â
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?
linux bash script tar
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?
linux bash script tar
linux bash script tar
asked Nov 21 '12 at 3:09
Jay LaCroix
366134
366134
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
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
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
Anytime @JayLaCroix - Welcome to SU!
â nerdwaller
Nov 21 '12 at 3:40
1
It is recommended to use$(command)instead of`command`.
â andrybak
Jan 25 '15 at 10:46
add a comment |Â
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
/
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
This doesn't work for me like eg. inalias ub='source ~/.bash_aliases && source $HOME/.bash_aliases && echo "aliases updated."';
â TheDefinitionist
Oct 5 '16 at 17:12
1
@TheDefinitionist Sounds like a different problem to this one. Perhaps open a new question?
â Paul
Oct 6 '16 at 2:58
 |Â
show 2 more comments
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
/
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
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
Anytime @JayLaCroix - Welcome to SU!
â nerdwaller
Nov 21 '12 at 3:40
1
It is recommended to use$(command)instead of`command`.
â andrybak
Jan 25 '15 at 10:46
add a comment |Â
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
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
Anytime @JayLaCroix - Welcome to SU!
â nerdwaller
Nov 21 '12 at 3:40
1
It is recommended to use$(command)instead of`command`.
â andrybak
Jan 25 '15 at 10:46
add a comment |Â
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
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
edited 21 hours ago
Pablo Bianchi
17010
17010
answered Nov 21 '12 at 3:14
nerdwaller
12.1k12839
12.1k12839
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
Anytime @JayLaCroix - Welcome to SU!
â nerdwaller
Nov 21 '12 at 3:40
1
It is recommended to use$(command)instead of`command`.
â andrybak
Jan 25 '15 at 10:46
add a comment |Â
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
Anytime @JayLaCroix - Welcome to SU!
â nerdwaller
Nov 21 '12 at 3:40
1
It is recommended to use$(command)instead of`command`.
â andrybak
Jan 25 '15 at 10:46
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
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
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
Anytime @JayLaCroix - Welcome to SU!
â nerdwaller
Nov 21 '12 at 3:40
Anytime @JayLaCroix - Welcome to SU!
â nerdwaller
Nov 21 '12 at 3:40
1
1
It is recommended to use
$(command) instead of `command`.â andrybak
Jan 25 '15 at 10:46
It is recommended to use
$(command) instead of `command`.â andrybak
Jan 25 '15 at 10:46
add a comment |Â
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
/
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
This doesn't work for me like eg. inalias ub='source ~/.bash_aliases && source $HOME/.bash_aliases && echo "aliases updated."';
â TheDefinitionist
Oct 5 '16 at 17:12
1
@TheDefinitionist Sounds like a different problem to this one. Perhaps open a new question?
â Paul
Oct 6 '16 at 2:58
 |Â
show 2 more comments
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
/
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
This doesn't work for me like eg. inalias ub='source ~/.bash_aliases && source $HOME/.bash_aliases && echo "aliases updated."';
â TheDefinitionist
Oct 5 '16 at 17:12
1
@TheDefinitionist Sounds like a different problem to this one. Perhaps open a new question?
â Paul
Oct 6 '16 at 2:58
 |Â
show 2 more comments
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
/
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
/
answered Nov 21 '12 at 3:15
Paul
47.8k13121147
47.8k13121147
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
This doesn't work for me like eg. inalias ub='source ~/.bash_aliases && source $HOME/.bash_aliases && echo "aliases updated."';
â TheDefinitionist
Oct 5 '16 at 17:12
1
@TheDefinitionist Sounds like a different problem to this one. Perhaps open a new question?
â Paul
Oct 6 '16 at 2:58
 |Â
show 2 more comments
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
This doesn't work for me like eg. inalias ub='source ~/.bash_aliases && source $HOME/.bash_aliases && echo "aliases updated."';
â TheDefinitionist
Oct 5 '16 at 17:12
1
@TheDefinitionist Sounds like a different problem to this one. Perhaps open a new question?
â Paul
Oct 6 '16 at 2:58
Dang it @Paul! Just beat me :D
â nerdwaller
Nov 21 '12 at 3:17
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
@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
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
This doesn't work for me like eg. in
alias ub='source ~/.bash_aliases && source $HOME/.bash_aliases && echo "aliases updated."';â TheDefinitionist
Oct 5 '16 at 17:12
This doesn't work for me like eg. in
alias ub='source ~/.bash_aliases && source $HOME/.bash_aliases && echo "aliases updated."';â TheDefinitionist
Oct 5 '16 at 17:12
1
1
@TheDefinitionist Sounds like a different problem to this one. Perhaps open a new question?
â Paul
Oct 6 '16 at 2:58
@TheDefinitionist Sounds like a different problem to this one. Perhaps open a new question?
â Paul
Oct 6 '16 at 2:58
 |Â
show 2 more comments
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
/
add a comment |Â
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
/
add a comment |Â
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
/
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
/
answered Jan 17 at 22:03
Alter Lagos
1214
1214
add a comment |Â
add a comment |Â
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid â¦
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid â¦
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f508507%2flinux-bash-script-single-command-but-multiple-lines%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown