Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers 2

up vote 6 down vote accepted

~/.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
share|improve this answer
    
env - /path/to/command is a standard compliant syntax, unless I misunderstand what you means. –  jlliagre Nov 2 '11 at 21:59
    
@jlliagre It is? Oh, it was but now it's been withdrawn in favor of env -i. –  Gilles Nov 2 '11 at 22:05
    
@Gilles: As usual your explanations are perfect. Is there a way to publicly thank you for everything you've (directly and indirectly) taught me in the course of the past three months? :) –  rahmu Nov 2 '11 at 22:34
    
@rahmu Use your newfound knowledge or your knowledge of other topics to provide good answers to other people's questions. Or otherwise make the Internet a better place. Or otherwise make the world a better place. –  Gilles Nov 3 '11 at 0:30
    
@Gilles thanks for clarifying that point. –  jlliagre Nov 3 '11 at 6:18
add comment
me@local:~ $ env - /path/to/shell
$ env
_=/usr/bin/env
PATH=/usr/bin:/bin
RANDOM=24395
$

Notes:

  • I used a shell to have a look at the resulting environment, any command can be used (guess what env - /usr/bin/env returns)
  • (@jlliagre's comment:) env is a POSIX standard command.

Edit Clarified the answer.

share|improve this answer
    
Unfortunately I couldn't find it on Solaris. –  rahmu Nov 2 '11 at 16:11
    
@rahmu According to the man pages (I don't have Solaris 10 to check), all versions of Solaris since at least 2.4 support env -. Run /usr/bin/ksh and not mksh, of course. –  Gilles Nov 2 '11 at 21:43
    
Indeed, env is a POSIX standard command so definitively in Solaris (including 10 and newer). –  jlliagre Nov 2 '11 at 21:57
    
Oh I got mislead by the mksh. I thought it was an executable that "cleaned" the environment :-/ In this case, the answer seems correct. –  rahmu Nov 2 '11 at 22:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.