Create more resilient PowerShell scripts on Linux
I had previously written this post on how to execute PowerShell scripts from Bash. In that post, I discussed how to add the shebang at the start of your script that will be ignored on non-Linux systems, but on Linux systems, it will provide the path to the interpreter you want to use, in our case it would be PowerShell. Here is a simple example:
#! /usr/bin/pwsh Write-Host 'Hello from a PowerShell script.'
So when this script executes, it will use the /usr/bin/pwsh location as the interpreter. That is all great if you did a distribution package installation like using the Debian or RPM package. However, if you use the Snap installation, the location for PowerShell is different. If I use the which command on a system with the Snap package for PowerShell installed I get the following location:
$ which pwsh /snap/bin/pwsh
Hmm, our script above will not work because the interpreter is defining an incorrect location. Fortunately, Linux provides a mechanism for handling this type of scenarios. This mechanism is the env command which allows you to pass an executable and it will lookup the interpreter via the PATH variable. Let's take a look at that is happening.
First let's look at our path:
$ echo $PATH /home/phillipsj/.npm-packages/bin:/home/phillipsj/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/phillipsj/.dotnet/tools
In my PATH you will see that the /snap/bin directory is listed along with /usr/bin. PowerShell will be installed in either one of these locations. So we can use the env command to determine which interpreter to use without hard coding the path. Here is what that is going to look like in the script above:
#! /usr/bin/env pwsh Write-Host 'Hello from a PowerShell script.'
With just this simple change, our PowerShell scripts will be able to use this method that PowerShell can be installed making those scripts more portable.
I hope you find this helpful.
Thanks for reading,