Wednesday, January 28, 2015

Linux script to replace environment variables

I searched and could not find a good solution to replace all environment variables on linux. So below is my version (simple, reasonably fast). Enjoy.

#!/bin/bash
infile=$1
now=$(date +"%s")
tmpFile=~\$infile.$now.tmp

# Prepare the echo command file to replace environment variables
echo > $tmpFile

OLD_IFS="$IFS"
IFS=
while read line
do
    echo "printf \"$line\n\"" >> $tmpFile;
done < $infile
IFS="$OLD_IFS"

source $tmpFile
rm $tmpFile