/usr/share/doc/apache2-doc/cgi-examples/printenv: print "Content-type: text/plain; charset=iso-8859-1\n\n"; charset assumptions? But mainly, this can be simplified to: for (sort keys %ENV) { $val = $ENV{$_}; $val =~ s|\n|\\n|g; $val =~ s|"|\\"|g; print "$_=\"${val}\"\n"; }