Bash script and two variables
Hi
I use a bash script: Code:
DATE="$(/bin/date +%d-%m-%Y)" like this: Code:
myfiles_09-11-2014_Time_15-30.tgz How can i do this? Thanks |
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 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 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. |
you can also write:
Code:
NAME="$(/bin/date +myfiles_%d-%m-%Y_Time_%H-%M)" |
Thanks both of you :)
|
Please remember to mark as SOLVED once you have a solution
|
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.
|
You could also set the variables apart with quotation marks, for example:
Code:
echo "$DATE"_Time_"$TIME" |
All times are GMT -5. The time now is 07:53 AM. |