6

So, I need to prevent loads of exported variables to be recognized by a child shell.

The problem is an application (Wine+UDK) won't build projects with too many (>65535) bytes on the environment, and mine has a lot (95323) after I use a project (ScriptEchoColor) that shares environment variables between any bash shells.

I can't find a command that will allow me to spawn a child shell that won't accept current environment variables and instead will just use the ones that are set on my user profile and on the system as a whole.

I tried many options shown at bash --help.

3 Answers 3

8

You can use the env command to start a process with a clean environment. Here is an example:

env -i /bin/myprog

If you still need to export some variables, you can do so in the call:

env -i MYVAR=foo /bin/myprog

The env command is required by POSIX.

1
  • perfect! indeed I had to set a few variables and was very simple like: env -i WINEPREFIX=$WINEPREFIX PATH=$PATH wine UDK.exe editor thx! Commented Sep 30, 2013 at 23:01
4

env -i somecommand runs somecommand in an empty environment. You can put arguments after somecommand. If PATH is erased, you'll need to specify the full path to somecommand. Alternatively you can preserve PATH or set a different value.

env -i PATH="$PATH:/another/directory" somecommand

If you want to have the same environment that you would have after login, preserve just a few variables and load your .profile to set the rest. If you use a different login shell (bash with .bash_profile, csh with .login, etc.), adapt accordingly.

env -i HOME="$HOME" PATH=/usr/local/bin:/usr/bin:/bin USER="$USER" \
    sh -c '. ~/.profile; "$@"' _ somecommand

Alternatively, you may prefer to remove some variables from the environment. env -u VAR1 -u VAR2 somecommand removes VAR1 and VAR2 from the environment and runs somecommand. Under Linux, you can use env -0 | tr '\n\0' '.\n' | sed 's/=.*//' to produce a list the environment variables with one name per line, suitable for filtering. On other unix variants, env | sed 's/=.*//' does the same job, with the limitation that it may find false positives if some variables's values contain newlines.

0
0

Issue typeset +x variable_name... or declare +x variable_name... command. You can even use regular expressions for variable names, like typeset +x ${!MY_VAR_*}.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .