5

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?

SAURABH SINGH
  • 121
  • 1
  • 1
  • 6
  • 1
    Please refer to these links https://stackoverflow.com/questions/714877/setting-windows-powershell-environment-variables and https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-7.1 – Kartik Chauhan Jun 17 '21 at 11:20
  • 1
    Does this answer your question? [Setting Windows PowerShell environment variables](https://stackoverflow.com/questions/714877/setting-windows-powershell-environment-variables) – Kartik Chauhan Jun 17 '21 at 11:20
  • Please update your question to clarify the `.sh` / batch-file confusion and what you're trying to achieve: process-level vs. persistent environment variables, who is expected to see the variables. – mklement0 Jun 17 '21 at 13:31
  • @mklement0 Hi actually I don't want a persistent environment variable. SETX can be used for it. I want to set the env variable temporarily so using the SET command, and actually, it runs well when running on the cmd line. But if putting the set command in the .sh file, it fails. – SAURABH SINGH Jun 17 '21 at 18:10

2 Answers2

8

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.

    • Note: While you can call a .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.

mklement0
  • 382,024
  • 64
  • 607
  • 775
0

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.

SAURABH SINGH
  • 121
  • 1
  • 1
  • 6
  • As an aside: while filename extension `.bat` works too, `.cmd` is generally preferable - see [this answer](https://stackoverflow.com/a/12011048/45375). – mklement0 Jun 17 '21 at 20:04