Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
636 views
in Technique[技术] by (71.8m points)

batch file - Start new cmd.exe and NOT inherit environment?

If I start a new CMD shell from an existing shell, the new shell inherits the existing environment. Is there a way to start a new shell but have it initialized to the system defaults and not inherit?

Current result:

B:>set _test=blooharky

B:>cmd
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

B:>set _
_test=blooharky

Desired result:

B:>set _test=blooharky

B:>cmd /env=default
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

B:>set _
Environment variable _ not defined

[update] Solution for this is start /i cmd, as shared by dbenham, below. However it doesn't help in the situation where the current shell is already second generation. Example:

d:>set _
Environment variable _ not defined

d:>set _test=blooharky

d:>cmd /k

:: some work done using _test here...
:: ...but after we need a new clean shell:

d:>start /i cmd

d:>set _
_test=blooharky

:: uhoh, our shell isn't clean!
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Anders' recipe has detailed control over a new environment, and I'm leaving that as the accepted answer because of that depth control. However one phrase in that answer led to me the method I'll actually be using for the particular problem which prompted this enquiry. An "environment that is equal to the initial explorer.exe environment". Oh! I know how to do that!

start /i "%windir%explorer.exe" "%windir%system32cmd.exe"

Note: originally this was simply start /i explorer ..., but it turns out that doesn't work reliably, and a full path should be be used for both parts. If all you need is a solution, you have everything now and can ignore the rest.


Following Ander's comment below about a security warning I tested on 4 Win7 computers, a mix of Pro, Home, and Enterprise, 32 and 64bit. I fiddled with User Account Control, all the way up and all the way down. Also tested with a non-administrator Command Prompt shell. These all worked without error or warning.

On Server 2003, logged on as local administrator, I get a popup dialog "File Download - Security Warning: do you want to run or save this file?" with buttons for run,save, and cancel. Pressing [run] results in an additional dialog, "Windows Explorer - Security Warning: the publisher could not be verified. Are you sure you want to run this software?". Pressing [run] again finally results in a usable command shell. Apparently there is some weird logic that results in Internet Explorer being substituted for Windows Explorer without the full path to the exe.

It gets even stranger though: using start with a title, (some advise to always include a title) results in the security warnings regardless of full path. Other parameters like starting directory seem to be okay.

:: security warning
start "clean shell" /i "%windir%explorer.exe" "%windir%system32cmd.exe"

:: is okay
start /i  /d x: "%windir%explorer.exe" "%windir%system32cmd.exe"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...