Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a shell script that exports values of variables when executed. The same values will be used in another script.
How to run this script(test.sh) in cron.

export I="10"
echo $I

I will be using root access for cron. I tried this command :

*/5 * * * * /home/ubuntu/backup/.test.sh

I checked with environment variables, nothing is updated.

share|improve this question
Exporting variables makes them visible to their children not to the parent shell. How is another script called? From within test.sh or from the parent shell? – Marco Feb 18 '13 at 12:27
test.sh is one script that exports the value. test2.sh is another script the uses the value of I stored as environment variable – Rudra Feb 18 '13 at 12:32
It depends on how does test2.sh get called? If its called by test.sh, then that should be ok. Otherwise it will probably not take over these variables. – mauro.stettler Feb 18 '13 at 12:40

Environment variables can only be passed down between a process and a direct process child. It cannot change or export environment variables to different process trees. Further, the exported variable is only available to process again that are direct descendents of the current process, NOT at the same level as the export is done.

share|improve this answer

In fact variables aren't shared across scripts in cron. Even if you define an environment variable like it says in the manpage if you have a script that changes that variable it won't really change for the second script. However, you can use a temporary file (you can even create it in memory if you don't want to rewrite a file on disk) to use between scripts.

For examle:

10 * * * * /path/to/script1.sh
20 * * * * /path/to/script2.sh

Contents of script1.sh:

echo "VAR1='VALUE1'" > /dev/shm/cronsharedfile

Contents of script2.sh:

source /dev/shm/cronsharedfile
do_something_with $VAR1

In this case the second script will have the correct value assigned by script1.sh. I used a file kept in shared memory (/dev/shm/cronsharedfile) but you can create a file on disk (/path/to/whateverfile) .

share|improve this answer

If there are 2 scripts in picture, then the variable exported in the first one would be visible to the next one. So, if you have a cron entry like

*/5 * * * * ( test1.sh; test2.sh ) > log.txt

And test1.sh as

export I="10"
echo $

Then $I would be visible in test2.sh.

share|improve this answer
What? No. Whatever test1.sh does in its environment has no impact on test2.sh. If you wanted that, you'd need to write . test1.sh; . test2.sh. – Gilles Feb 20 '13 at 20:00
hmm. May be I had tried the way you showed. I guess sourcing is then really different that running a program from a PATH directory ? – mtk Feb 21 '13 at 7:03
Yes, sure, sourcing is different. . test1.sh tells the current shell to parse and execute the commands in test1.sh. test1.sh runs the external program test1.sh which may or may not be a shell script. – Gilles Feb 21 '13 at 16:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.