LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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, 06: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 06:35 PM.
 
Old 11-09-2014, 03:36 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,499

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
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, 04:59 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,216

Rep: Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696
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, 07:41 AM   #4
ASTRAPI
Member
 
Registered: Feb 2007
Posts: 210

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

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

Rep: Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066
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, 09:36 PM   #7
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 329

Rep: Reputation: 126Reputation: 126
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 09: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 08:37 AM
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 06:09 AM
bash script - get filename created by script using variables aolong Linux - General 8 11-19-2009 12:40 PM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 11:33 AM
bash script variables twantrd Programming 7 11-17-2004 02:38 AM


All times are GMT -5. The time now is 05:50 AM.

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