Python script setting a shell environment variable

os.environ is a useful way to set an environment variable in the shell as shown below:

#!/usr/bin/python
import pdb
import os
import sys
import subprocess
import commands
import time
import socket
import locale
locale.setlocale(locale.LC_ALL, 'en_US')
 
team_name = 'Delta Team'
msg_line = 'The '+team_name+' is executing this script.'
os.environ['msg_line'] = msg_line
os.system('echo "${msg_line}" | mail -s "Email subject goes here" "Dean.Capps@somecompany.com"')

(I may have extra imports as this was taken from a much larger script.)

Author: Dean Capps

Database consultant at Amazon Web Services.