It looks like you're looking for an equivalent of the following syntax commonly used in Unix (POSIX-compatible) shells such as Bash, which allows you to define an "ephemeral" environment variable that exists for a given command (child process) only:
# Unix shells such as Bash only:
# Create a command-scoped PORT environment variable with value 3000
# It is *only* defined for the child process created by the call.
PORT=3000 ./node_modules/.bin/env-cmd -f .env.crkeng react-scripts start
Neither cmd.exe
nor PowerShell offer such a syntax, and these two shells fundamentally have very different syntax.[1]
- This applies as of PowerShell 7.3.x, though there is a proposal to introduce support for such "ephemeral" environment variables in a future PowerShell version - see GitHub issue #3316.
Instead, you must define the variable first, in a separate statement and then call the target program.
- You may also want to undefine the variable / restore its previous value afterwards.
Concrete solutions:
Note:
- The following uses your command as specified in your question. It seems that there may be a problem with that command (possibly,
npx env-cmd -f .env.crkeng react-scripts start
is the correct form), which, however, is incidental to the solutions below with respect to specifying the environment variable of interest.
Without undefining / restoring afterwards (env. var. PORT
will linger, with value 3000
):
cmd.exe
(note the need to use \
as the path separator):
:: Must be *separate statements* on *separate lines*
:: Enclosure in "..." isn't strictly needed, but a good habit to form.
set "PORT=3000"
.\node_modules\.bin\env-cmd -f .env.crkeng react-scripts start
- If a program path contains spaces, enclose it in
"..."
PowerShell (which accepts \
and /
interchangeably):
# *Separate statements*, either on separate lines or separated with ";"
$env:PORT = 3000; ./node_modules/.bin/env-cmd -f .env.crkeng react-scripts start
- If a program path contains spaces, enclose it in
"..."
(expandable string) or '...'
(verbatim string) and invoke it via &
(e.g., & './path with spaces/some.exe' ...
)
With undefining / restoring afterwards:
cmd.exe
: create a temporary copy of the environment with setlocal
/ endlocal
setlocal
set "PORT=3000"
.\node_modules\.bin\env-cmd -f .env.crkeng react-scripts start
endlocal
PowerShell: manually save and restore the previous value
$prevPort = $env:PORT; $env:PORT = 3000
./node_modules/.bin/env-cmd -f .env.crkeng react-scripts start
$env:PORT = $prevPort
- Note:
- If env. var.
PORT
didn't previously exist, $prevPort
contains $null
, and assigning $null
to $env:PORT
later effectively undefines (removes) it.
[1] Unfortunately, the creators of PowerShell, in a well-meaning effort to ease the transition from cmd.exe
, the legacy shell, decided to create aliases named for the cmd.exe
's (internal) commands as well as for some standard executables (including those on Unix-like platforms). This is why set
- cmd.exe
's internal command for setting (what are invariably also environment) variables - also exists as a PowerShell alias for its Set-Variable
cmdlet, even though both the syntax and the purpose of these commands differ (Set-Variable
only sets shell-only variables) - see the bottom section of this answer for a discussion and PowerShell-idiomatic alternatives.