Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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( in cron.

export I="10"
echo $I

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

*/5 * * * * /home/ubuntu/backup/

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 or from the parent shell? –  Marco Feb 18 '13 at 12:27 is one script that exports the value. 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 get called? If its called by, then that should be ok. Otherwise it will probably not take over these variables. –  mauro.stettler Feb 18 '13 at 12:40

3 Answers 3

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

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 * * * * (; ) > log.txt

And as

export I="10"
echo $

Then $I would be visible in

share|improve this answer
What? No. Whatever does in its environment has no impact on If you wanted that, you'd need to write .; . –  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. . tells the current shell to parse and execute the commands in runs the external program which may or may not be a shell script. –  Gilles Feb 21 '13 at 16:29

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/
20 * * * * /path/to/

Contents of

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

Contents of

source /dev/shm/cronsharedfile
do_something_with $VAR1

In this case the second script will have the correct value assigned by 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

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.