Shell – How to Get a Clean Environment in ksh

environment-variableskshshell

I need to get rid of all the environment variables in a Ksh shell. I can fork a new instance, but it will inevitably source some init files (as far as I know .profile, .kshrc). Is there a way to bypass the sourcing of those files and any other file that might be read at init time?

  • Ksh version: Version M-11/16/88i
  • OS: Solaris 10

Hope I'm clear enough.

Best Answer

~/.profile is only read by login shells. ~/.kshrc is only executed for interactive shells.

Solaris's env supports the syntax (now deprecated, but retained in Solaris, which takes backward compatibility seriously) env - /path/to/command to run /path/to/command in an empty environment. So env - /usr/bin/ksh -c /path/to/script will run the script in an empty environment and will not source any profile script. Ksh might set some environment variables on its own initiative: I don't know about ksh88, but ksh93 sets _ and PWD, and pdksh sets _ and PATH.

You can selectively or indiscriminately clear environment variables from inside ksh.

unset x
for x in $(typeset +x); do
  unset $x
done
Related Question