I used to use Bash, which has this syntax:
VAR=VAL ./executable_file
to set VAR temporarily during the execution of executable_file.
Is there a corresponding syntax in pwsh?
PowerShell 7.4.0 on MS-Windows
After consulting the relevant documents, I found that Start-Process -Environment @{VAR = VAL} --args ... can achieve this.
$TEEEST=40
$env:TEEEST=41
Start-Process -NoNewWindow -FilePath "emacs.exe" -ArgumentList '-Q', '--batch', '--eval', `(message`(getenv`(symbol-name`'TEEEST`)`)`) -Environment @{TEEEST = 42} -Wait
echo $TEEEST $env:TEEEST
Output:
42
40
41
Note:
It currently only works in pwsh (PowerShell Core) v7.4.
You can't capture stdout/stderr like what you do in Bash (e.g., TEEEST=42 emacs -Q -nw --batch --eval '(print (getenv "TEEEST"))' > a.txt).
I tested this by
Start-Process -NoNewWindow -FilePath "emacs.exe" -ArgumentList '-Q', '--batch', '--eval', `(print`(getenv`(symbol-name`'TEEEST`)`)`) -Environment @{TEEEST = 42} -Wait > a.txt
As @mklement0 mentioned:
your only options are
-RedirectStandardOutput/-RedirectStandardErrorto send output to files, with no direct ability to merge the streams.
The $LASTEXITCODE is not set. I tested this by
echo $LASTEXITCODE
Start-Process -NoNewWindow -FilePath "gcc.exe" -Environment @{TEEEST = 42} -Wait
echo $LASTEXITCODE # You may expect it to be 1 because gcc didn't receive arguments.
output (omit the output of gcc)
0
0
pwsh versions, but it won't be back-ported to powershell (Windows PowerShell). The way to get the exit code is by adding -PassThru to the Start-Process call, which makes it output a process-info object whose .ExitCode property you can query. However, there's a bug that currently prevents this with -NoNewWindow - see GitHub issue #20400, which also mentions workarounds.
$env:VAR=VALto set an environment variable, but I need to unset it after execution (and it will cover the original value, which is not what I'm expecting).FOO=bar someutility …). [GitHub issue #3316] (github.com/PowerShell/PowerShell/issues/3316) is a request to support at least similar syntax. Current options: (a) Set env. vars. for the current process, call the child process (which inherits them), then restore the env. (b) Call via a shell (possible PowerShell itself) and set the vars. there - you pay a performance penalty. (c) In v7.4+, you may useStart-Process -NoNewWindow -Wait -Environment ...- you lose stream integration.