Windows CMD 中是否有与 bash 或 tcsh 中等效的源命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10402379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:29:48  来源:igfitidea点击:

Is there an equivalent source command in Windows CMD as in bash or tcsh?

windowsbashunixcmdtcsh

提问by mart2001

I know that in the unix world, if you edit your .profile or .cshrc file, you can do a source ~/.profile or source ~/.cshrc to get the effect on your current session. If I changed something in the system variable on Windows, how can I have it effect the current command prompt session without exiting the command prompt session and opening another command prompt session?

我知道在 unix 世界中,如果您编辑 .profile 或 .cshrc 文件,您可以执行 source ~/.profile 或 source ~/.cshrc 以获得对当前会话的影响。如果我在 Windows 上更改了系统变量中的某些内容,如何在不退出命令提示符会话并打开另一个命令提示符会话的情况下使其影响当前命令提示符会话?

采纳答案by johnshen64

I am afraid not, but you can start using Powershell, which does support dot sourcing. Since powershell window is really based on cmd so all your dos command will continue to work, and you gain new power, much more power.

恐怕不行,但您可以开始使用 Powershell,它确实支持点源。由于 powershell 窗口实际上是基于 cmd 的,因此您的所有 dos 命令都将继续工作,并且您将获得新的力量,更多的力量。

回答by Eike

In the usual Windows command prompt (i.e. cmd.exe), just using call mybat.batdid what I wanted. I got all the environment variables it had set.

在通常的 Windows 命令提示符(即 cmd.exe)中,只需使用即可call mybat.bat完成我想要的操作。我得到了它设置的所有环境变量。

回答by shellter

The dos shell will support .bat files containing just assignments to variables that, when executed, will create the variables in the current environment.

dos shell 将支持仅包含变量赋值的 .bat 文件,这些变量在执行时将在当前环境中创建变量。

  c:> type EnvSetTest.bat
  set TESTXYZ=XYZ

  c:> .\EnvSetTest.bat

  c:> set | find "TESTX"
  TESTXYZ=XYZ
  c:>

IHTH.

IHTH。

回答by Fathah Rehman P

Following example will help you to solve your problem.

以下示例将帮助您解决问题。

env.batThis file is for setting variables. Its contents are given blow.

env.bat该文件用于设置变量。其内容受到打击。

set name="test3"

test.batOur main batch file.

test.bat我们的主要批处理文件。

call env.bat
call print.bat
pause

Now print.batbatch file to print variables. Its contents given below

现在print.bat批处理文件来打印变量。其内容如下

echo %name%

回答by Ajay Gautam

The only way I have found this to work is to launch a new cmd window from my own config window. eg:

我发现它起作用的唯一方法是从我自己的配置窗口启动一个新的 cmd 窗口。例如:

@echo off
echo Loading...
setlocal enabledelayedexpansion
call 1.cmd
call 2.bat
...
...
if "%LocalAppData%"=="" set LocalAppData=%UserProfile%\Local Settings\Application Data
SET BLAHNAME=FILE:%LocalAppData%\BLAH
call blah blah
cmd

The last cmd will launch a new cmd prompt with the desired settings exported to the command window.

最后一个 cmd 将启动一个新的 cmd 提示符,并将所需的设置导出到命令窗口。

回答by will

Here's a workaroundfor some limited use-cases. You can read-in a file of commands and execute them in-line. For example the calling command file looks like:

这是一些有限用例的解决方法。您可以读入命令文件并在线执行它们。例如,调用命令文件如下所示:

echo   OFF
SETLOCAL  ENABLEDELAYEDEXPANSION
     :
echo.         ----------------
echo.           set-up java
echo.         ----------------
echo.
rem       call    %DEV_SCRIPTS%\setup-java
for /F "tokens=*" %%A in ( %DEV_SCRIPTS%\setup-java.bat ) do (
    %%A
)        
call    %DEV_SCRIPTS%\show-java
     :

In the setup-java.batfile you can't use %expansion. You need to use !; e.g.:

setup-java.bat文件中你不能使用%扩展。你需要使用!; 例如:

    set  JRE_HOME=!JRE_08!
    rem        
    set  JRE_TARGET=!JRE_HOME!

So you are litterally source-ing commands from a text file. You will need to test which commands sourcedin this way. It took a few trials just to set some environment variables.

因此,您几乎是source从文本文件中执行命令。您将需要测试,命令来源以这种方式。仅仅为了设置一些环境变量就花了一些试验。

I don't think we can do logic or loops because the command processor scans the file at the start. I am OK just having a simple workaround to reuse shared things like environment definitions. Most other things won't need an actual sourcecommand (I am hoping). Good luck.

我不认为我们可以做逻辑或循环,因为命令处理器在开始时扫描文件。我只是有一个简单的解决方法来重用共享的东西,比如环境定义。大多数其他事情不需要实际source命令(我希望)。祝你好运。

回答by elGuru

For example to set VC# vars

例如设置 VC# vars

C:\Windows\System32\cmd.exe /k "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"

回答by Adwait_07

Use git bash for windows, it works totally fine!

在 Windows 上使用 git bash,它完全正常!