global date for many scripts in shell

Hi,
I am using shell (#!/bin/bash), i am trying to set my date in a script date.sh, i want this dates to be used by another scripts.

I have tried to make:
#!/bin/bash
begin_date =`date +%Y_%m_%d_%H_%M_%S`,

i also tried:
#!/bin/bash
begin_date =20080709 12:14:11, then i write in other script the date as ($begin_date), but it always gives me begin_date : command not found!

I would like to write the date as numbers like date = 20080721 12:14:11 and then the other script should read it.
I think it should be simple, please can you help me how i can assign this date and time and how i should call it from my other scripts?

Thank you in advance.

Try exporting begin_date

You need to export the variable if you want it to be available to the child shells. Change your date.sh script as

#!/bin/bash
begin_date =`date +%Y_%m_%d_%H_%M_%S`
export begin_date

Hi,
it still giving me begin_date: command not found.
here is how i changed in date.sh :

#!/bin/bash
begin_date =`date +%Y_%m_%d_%H_%M_%S`
export begin_date

i want to change the date from script to script, so i am tryingto make this script to change the date manually, from today to next month etc... i would like to set like 3 variables for example:
begin_date1 = 2008-07-07_11:12:12 (sysdate)
begin_date2 = 2008_08-08 14:13:34 (next month)
etc ...

How are you calling the date.sh script from the other scripts? Can you please post that. I tried the following and it worked:

The following is the date.sh script. Please note that there is no export command in this:

#!/bin/sh
begin_date=`date +%Y_%m_%d_%H_%M_%S`

The following is prog1.sh where I want to use the begin_date variable. I have sourced the date.sh script in the prog1.sh script:

#!/bin/sh
# Source the date.sh script
. ./date.sh
echo $begin_date

Now when I execute prog1.sh as follows:

$ ./prog1.sh
2008_07_07_16_18_18

Just for clarification, the leading "." in line no 3 in prog1.sh is for sourcing a script within another script, and the ./date.sh is to specify that the date.sh is in the current directory (the same directory where the prog1.sh is located), if you have placed the date.sh in some other location such as /home/user01/comm_scripts, you can replace the ./ with the full path of the script.

Hi,
Thank you very much for your kind explanation. it works now.
but can you tell me how i can decide my own date, if i decide a date and time different than sysdate it gives me command not found. i would like to precise my own special dates and time like next month 2008-08-09 00:00:02 etc.

i am trying like this, but doesn't work:
#!/bin/sh
begin_date=`2008_08_09_11_12_14 +%Y_%m_%d_%H_%M_%S`

or

#!/bin/sh
begin_date=`2008_08_09_11_12_14`

Thanks in advance, i appreciate it.

The back ticks (also known as grave-accent) you are using means that the contents are to be executed and their result placed in its place, so the lines:

begin_date=`2008_08_09_11_12_14 +%Y_%m_%d_%H_%M_%S`

or

begin_date=`2008_08_09_11_12_14`

are incorrect as 2008_08_09_11_12_14 +%Y_%m_%d_%H_%M_%S is not a command in unix and neither is 2008_08_09_11_12_14.

If you want to specify your own date then you can do that as follows:

begin_date="2008_08_09_11_12_14"

HTH.

hi,
Thank you very much, it works great now, but i still have 1 question, i am really very new in shell and seems the date has special case in using. I am trying to use this begin_date in other script, but i don't want it to read in echo, it works good when i use echo, but i want it to be as variable in another script. so i want time = $begin_date in script2.

#!/bin/sh
#begin_date=`date +%Y-%m-%d_%H:%M:%S`
begin_date="2008-08-09_11:12:14"

in script 2:
#!/bin/bash
. ./date.sh
echo $begin_date (this one gives me the right date in date.sh)
time = $begin_date

the variable time inside the script i use as $time, but it gives me sysdate.

thanks again.
Regards.

Also
a = b is wrong in shell scripting
a=b is correct - no spaces around the equal sign

Hello,
I have fixed the space, but i can not know what i am doing wrong or what i am missing,

date.sh
#!/bin/sh
begin_date="2008-08-05_11:12:14"

in script 2:
#!/bin/bash
. ./date.sh
echo $begin_date
time=$begin_date

is it right to use $time inside the script, why it doesnt take it the date i am assigning in date.sh script?

date.sh

#!/bin/bash
begin_date='2008-08-05_11:12:14'

:b::D:):smiley:

Thank you very much for all the people who replies me. it finally worksss.

I appreciate it a lot.
Regards.