LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-08-2014, 07:23 PM   #1
ASTRAPI
Member
 
Registered: Feb 2007
Posts: 210

Rep: Reputation: 16
Question Bash script and two variables


Hi

I use a bash script:

Code:
DATE="$(/bin/date +%d-%m-%Y)"
TIME="$(/bin/date +%H-%M)"
/bin/tar -czf myfiles_$DATE_Time_$TIME.tgz file.zip
And i want on the outpout to have inside the date and time the word Time so i will not get all numbers together....

like this:

Code:
myfiles_09-11-2014_Time_15-30.tgz
But it seems that it doesn't like the _Time_ as it is not working with it there and when i remove it is fine...

How can i do this?

Thanks

Last edited by ASTRAPI; 11-08-2014 at 07:35 PM.
 
Old 11-09-2014, 04:36 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
Your issue is that an underscore is part of valid variable name, hence when you pass the string to tar it looks like:
Code:
myfiles_$DATE_Time_$TIME.tgz
As you can see from the colours, you have no variable called $DATE_Time_

When you have additional parts that need to be attached though, bash has the curly brace enclosure to assist with sectioning off the variable names.
So you can use:
Code:
myfiles_${DATE}_Time_$TIME.tgz
Now this will only look for $DATE and will append _Time_.

The curly brace enclosure can actually be used around all variables all the time. It does make your code cleaner however to only use them when required.
 
Old 11-09-2014, 05:59 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,514

Rep: Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436
you can also write:
Code:
NAME="$(/bin/date +myfiles_%d-%m-%Y_Time_%H-%M)"
/bin/tar -czf $NAME.tgz file.zip
 
Old 11-09-2014, 08:41 AM   #4
ASTRAPI
Member
 
Registered: Feb 2007
Posts: 210

Original Poster
Rep: Reputation: 16
Thanks both of you
 
Old 11-09-2014, 09:47 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
Please remember to mark as SOLVED once you have a solution
 
Old 11-09-2014, 09:50 AM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,362

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
As a quick note on convention, all caps is generally reserved for environment variables. It's somewhat frowned upon to use names in all caps for regular variables in a script, because it can lead to confusion. Instead, regular variables should be lower case or mixed case.
 
Old 11-09-2014, 10:36 PM   #7
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 314

Rep: Reputation: 123Reputation: 123
You could also set the variables apart with quotation marks, for example:
Code:
echo "$DATE"_Time_"$TIME"
09-11-2014_Time_22-33

Last edited by Beryllos; 11-09-2014 at 10:38 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 09:37 AM
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 07:09 AM
bash script - get filename created by script using variables aolong Linux - General 8 11-19-2009 01:40 PM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 12:33 PM
bash script variables twantrd Programming 7 11-17-2004 03:38 AM


All times are GMT -5. The time now is 10:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration