$ENV{P2KTMP} = '/foo/bar'; my $consolidatedPath = '$P2KTMP/data'; my $unsafe = '$P2KTMP/data'; # Method 1 my @ALLOWED_VARS = qw/ P2KTMP FOO BAR /; my $VAR_RE = join '|', map {quotemeta} @ALLOWED_VARS; $consolidatedPath =~ s/\$($VAR_RE)\b/$ENV{$1}/g; # Method 2 chomp( $unsafe = `echo $unsafe` ); print "$consolidatedPath\n"; print "$unsafe\n"; __END__ /foo/bar/data /foo/bar/data