You can pass values via environment variables, which Bash (and POSIX-compatible shells in general) makes easy:
$ foo='BAR'; bar='BAZ' # define sample Bash shell vars.
$ foo="$foo" bar="$bar" pwsh -noprofile -command '$env:foo; $env:bar'
BAR
BAZ
Note how prepending foo="$foo"
and bar="$bar"
defined shell variables $foo
and $bar
as process-scoped environment variables for the pwsh
call.
That is, these environment variables are only in effect for the pwsh
child process.
Alternatives:
You can define environment variables separately, beforehand - e.g.,
export foo='BAR'
, but note that these stay in effect for the lifetime of the current process that the bash
script runs in (unless you undefine them later).
To make all variables in your bash
script environment variables automatically (again for the lifetime of the current process), you can place a set -a
statement before you create your regular shell variables. Then a regular assignment such as foo='BAR'
automatically creates foo
as an environment variable that PowerShell can access as $env:foo
.