I have written about 10 scripts that create individual log files of tasks on my Raspberry Pi4 (OS is Ubuntu Server). These scripts are activated via crontab, ssh query my 12 Raspberry Pi MotionEye security cameras, and report back what they have done in created log files and email messages. The scripts activate at different cron times (however, 3 of them run on the hour with 2 of those only running at midnight), but will sometimes overlap when running. Currently, the scripts are pretty much self sustaining. What I would like to do is create a sub-script that does the emailing so I don't have to keep up with that task in each main script should I make any changes. To test this, I created 7 test script files, TestFileA - TestFileG that write varying lines of text to their own individual log files (6, 9, 12, 4, 10, 12, & 4 lines):


export TLOG=/home/.myaccountname./TestDir/TestFileD.tmp
echo "This TestFileD log contains 2 additional lines" >| $TLOG
echo "TestFileD, Line 1" >> $TLOG
echo "$(date "+%m-%d-%Y %T"): TestFileD Complete" >> $TLOG
cat $TLOG >> $ALOG
echo "MailFile: "$MailFile >> $ALOG
rm $TLOG

Each script then ends with assigning the temporary individual log file name to the same $TLOG variable and calls on the SendLog sub-script, which is basically:


cat /home/.myaccountname./TestDir/_EmailHeader $TLOG >| $MailFile
cat $MailFile | /usr/sbin/ssmtp .myemailaddress.@gmail.com
rm $MailFile

Finally, I created a crontab to execute all 7 test scripts at the exact same time once an hour. I did this to make sure if I wrote this into my "live" scripts, that there would not be any conflicts from the same "sub-script" being accessed by multiple scripts using the same variables at the same time. So far, the first 2 times the crontab executed, I only received 5 emails. The latest (3rd) time this executed, I did receive all 7 emails. (Each time, all 7 logs files are properly created). Does anyone see anything wrong with what I am doing before I write into the live scripts?

  • Apart from the general advice of quoting your variables ("$MailFile", "$TLOG", "MailFile: $MailFile"...)—which is not an issue here, but may cause you headaches should you choose, or maybe automatically generate, file names with blank characters or newlines—you may want to log errors from your scripts (invoking them as script 2>/path/to/err.log). Also, note that MailFile is never set in your main script; not even after SendLog.sh returned.
    – fra-san
    Apr 16, 2020 at 21:51
  • Thank you fra-san. '$MailFile' is not set at the top level main script, because it's limited to the few lines of code in 'SendLog.sh'. It's only purpose is to create a '.txt' file for 'ssmtp'ing after 'cat'ing the mail header file to the '.log' file. As soon as the '.txt' file is emailed, it is erased and '$MailFile' not called again. Are you saying that it should still be "set" at the top level?
    – Dewey
    Apr 17, 2020 at 0:34
  • I'm saying that, as stated in the answer you have, a value assigned in a subscript (SendLog) never becomes visible to the main (or caller) script. I.e. echo "MailFile: "$MailFile won't print the value given to the variable in SendLog.
    – fra-san
    Apr 17, 2020 at 9:06
  • I see your point... I forgot that I had left echo $MailFile in the main script when I posted the question. It was only there temporarily to show what the value was when the sub-script finished processing (it was set when I was testing the script) .
    – Dewey
    Apr 17, 2020 at 11:57

1 Answer 1


Variables are not shared between scripts. Environment variables are copied to child processes (when the child is started). The information dose not flow back, or sideways, it does not flow after the process is started.

It is like two people following the same recipe.
A process has two parts:

  • The recipe that it follows: The code stored on secondary-storage (disk). The process can not change this. It can be shared between processes (because it is immutable, there are no side effects of sharing). It is like they both get a copy (there own recipe book).
  • Its state: Variable etc. It can change this. It is not shared. Some initial state comes from the recipe, or from the parent. However changes are not shared. It is like they both make notes on a bit of paper. Both people also keep track of how far they have got (the program counter). (None of these are shared).

As for e-mail, that would depend on the e-mail sending code.

  • Thank you. I think this is what I am thinking and what I am looking for. So if I'm understanding you correctly, if Script A and Script B call on Script C at the exact same time, Script C will still respect/follow only the respective calling script's variable assignment despite the fact that both calling scripts are using the same variable name. Thanks again.
    – Dewey
    Apr 17, 2020 at 0:43
  • Yes. It is like two people following the same recipe. [I will add a note on this to answer] Apr 17, 2020 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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