Reputation: 151
There is a script.sh file
set FABRIC_CFG_PATH=<some path>
set CORE_PEER_LOCALMSPID=<some id>
If I'm running this script in windows, the env variables are not getting set.
Whereas if setting the env using the cmd approach,
E.g., on windows cmd
set FABRIC_CFG_PATH=<some path>
It works fine.
So how can I set the env in windows through a shell script file?
Upvotes: 8
Views: 26361
Reputation: 439812
Since your intent is to define current-process-only environment variables (rather than persistently defined ones, which on Windows are stored in the registry) you need to use a script file / batch file that runs in-process in order for environment variables defined therein to be seen by the script's caller.
Therefore:
If the caller is a cmd.exe
session, you must use a batch file: a plain-text file with filename extension .cmd
(or, less preferably, .bat
[1]) that uses cmd.exe
syntax.
If the caller is a PowerShell session, you must use a PowerShell script: a plain-text file with filename extension .ps1
that uses PowerShell syntax.
.cmd
file (batch file) from PowerShell too (but not directly vice versa), this will not work as intended, because of necessity it runs in a (cmd.exe
) child process, whose environment variables aren't seen by the PowerShell caller.As for .sh
files: they have no predefined meaning on Windows, but may be defined by third-party applications, such as Git Bash. In the case of the latter, invoking a .sh
file passes it to the POSIX-compatible Bash shell, which has its own syntax. More importantly, invoking such a file won't work as intended when called from either cmd.exe
or PowerShell, because Bash must run in a child process, and child processes cannot set environment variables for their parents.
cmd.exe
/ batch-file example:
Create a file named envVars.cmd
, for instance, and place the following lines in it:
@echo off
:: Note: Do NOT use `setlocal` here
set "FABRIC_CFG_PATH=C:\path\to\some directory\config"
set "CORE_PEER_LOCALMSPID=42"
Then, from your cmd.exe
session / another batch file, call the file as follows to make the environment variable-definitions take effect for the current process (assuming the file is in the current directory):
.\envVars.cmd
You will then able to refer to the newly defined variables as %FABRIC_CFG_PATH%
and %CORE_PEER_LOCALMSPID%
.
PowerShell example:
Create a file named envVars.ps1
, for instance, and place the following lines in it:
$env:FABRIC_CFG_PATH='C:\path\to\some directory\config'
$env:CORE_PEER_LOCALMSPID=42
Then, from a PowerShell session / another PowerShell script, call the file as follows to make the environment variable-definitions take effect for the current process (assuming the file is in the current directory):
./envVars.ps1
You will then able to refer to the newly defined variables as $env:FABRIC_CFG_PATH
and $env:CORE_PEER_LOCALMSPID
.
[1] See this answer.
Upvotes: 11
Reputation: 151
After some study on the executables/batch files in windows, I have come to the conclusion that I need to write a batch .bat file to use the set command to set the env variables as I desire.
Upvotes: 0