3

We have a project where we want to run some Bash & Powershell scripts on Linux. We will be using a BASH Script to run some processes with various variables, execute a powershell script & then complete some more BASH commands.

If we have variables set in the bash script and then we execute the powershell script in the same BASH session, is there a way to pass those variables across to Powershell?

ausip
  • 233
  • 1
  • 3
  • 7

2 Answers2

4

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • While this works, I've tried several combinations when using `-File` as opposed to `-Command`. This doesn't seem to work when using `-File`. Not a knock against this answer, though, because it _does_ work when using `-Command`. – fourpastmidnight Apr 28 '22 at 21:08
  • @fourpastmidnight, pass-through arguments to a `-File` script are by design _literal_ arguments; thus, the target script itself would have to use `$env:` references inside the script. You can find more info about `-File` vs. `-Command` in [this answer](https://stackoverflow.com/a/57443822/45375). – mklement0 Apr 28 '22 at 21:16
  • Yup, understood that _inside_ a script, variables use the `$env:` prefix. But, when using environment variables to parameters to the file, e.g. `pwsh -File the-script.ps1 -Param1 $SOME_VAR`, nothing seemed t work correctly. Let me clarify, _this_ didn't seem to work: `SOME_VAR="a value" pwsh -File the-script.ps1 -Param "$SOME_VAR"`. – fourpastmidnight May 02 '22 at 14:08
  • 1
    @fourpastmidnight, your approach would only work if you defined `SOME_VAR="a value"` _in a separate statement_ (and therefore only as a _shell_ variable). Your command is conceptually flawed in that _Bash_ would try to expand `$SOME_VAR` _up front_ - but `$SOME_VAR` isn't defined in the calling shell (it is only defined as an ephemeral env. var. for the _child_ process being invoked). Try `FOO='not yet' /bin/echo "[$FOO]"` (on one line). – mklement0 May 02 '22 at 14:27
  • Interesting, because when I do this, it works: `SOME_VAR="a value" pwsh -Command "& { .\the-script.ps1 -Param1 \"\${env:SOME_VAR}\""`. (I just had _other_ issues using this approach not related to this specific thing at hand ;) ). – fourpastmidnight May 02 '22 at 15:48
  • If I understand what you're saying, after thinking about it a bit more, when using `-File` and "pre-creating" the environment variable `SOME_VAR`, in the _current shell_, `SOME_VAR` doesn't yet exist, and so using `-File the-script.ps1 -Param1 $SOME_VAR` doesn't work. The reason the `-Command` version works is because _we've already_ shelled to execute `pwsh`, and so in that subshell, `$SOME_VAR` exists. – fourpastmidnight May 02 '22 at 15:50
  • 1
    Correct, @fourpastmidnight (except that `pwsh` isn't a _subshell_ in the Bash sense, it is an unrelated child process that itself happens to be a (different) shell). – mklement0 May 02 '22 at 15:57
  • 1
    Thanks @mklement0, greatly appreciated. – fourpastmidnight May 02 '22 at 21:07
  • Glad to hear the explanations were helpful, @fourpastmidnight; my pleasure. – mklement0 May 02 '22 at 21:08
0

If you export a bash variable it will be available in powershell as $env:variablename:

example=/var/data
export example
pwsh -noprofile -command '$env:example;'

returns /var/data

In Powershell you can run Get-ChildItem Env: to list all environment variables.

Mike A
  • 500
  • 4
  • 16