I have several PowerShell scripts that I'd like to invoke directly as a command from a Bash shell in Cygwin. For example, if I write a script with the filename Write-Foo.ps1, I'd like to execute it as a command from any working directory:
$ Write-Foo.ps1 arg1 arg2 ...
To do this, I add the script to my PATH, make it executable, and include the following interpreter shebang/hashbang at the beginning of the file:
#!/usr/bin/env powershell Write-Host 'Foo' ...
It's a common (ab)use of the env utility, but it decouples the scripts from Cygwin's path prefix (/cygdrive/c/...), at least for the interpreter declaration.
This works to start PowerShell, but the system passes the file as a Cygwin-formatted path, which PowerShell doesn't understand, of course:
The term '/cygdrive/c/path/to/Write-Foo.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program.
MSYS (Git Bash) seems to translate the script path correctly, and the script executes as expected, as long as the path to the file contains no spaces. Is there a way to invoke a PowerShell script directly by relying on the shebang in Cygwin?
Ideally, I'd also like to omit the .ps1 extension from the script names if possible, but I understand that I may need to live with this limitation. I want to avoid manually aliasing or wrapping the scripts if possible.
A quick note for Linux/macOS users finding this.
As long as the script file itself doesn't contain spaces in the filename, this allows us to run the script from any working directory—no shebang needed. PowerShell uses a native routine to resolve the command from the PATH , so we don't need to worry about spaces in the parent directories.
Starting a Script With #! It is called a shebang or a "bang" line. It is nothing but the absolute path to the Bash interpreter. It consists of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash.
The shebang is a special character sequence in a script file that specifies which program should be called to run the script. The shebang is always on the first line of the file, and is composed of the characters #! followed by the path to the interpreter program.
Writing a Shell ScriptThe most common need for using shebang appears when we're writing shell scripts. “/bin/sh” is actually the symlink to an sh-compatible implementation of sh (Shell Command Language). In most cases, it'll be bash (Bourne-Again SHell), but if we want to secure portability, we should use the symlink.
Quick note for Linux/macOS users finding this:
PATH
#!/usr/bin/env pwsh
\n
, not \r\n
)Thanks to briantist's comments, I now understand that this isn't directly supported for PowerShell versions earlier than 6.0 without compromises:
...[in PowerShell Core 6.0] they specifically changed positional parameter 0 from
‑Command
to‑File
to make that work. ...the error message you're getting is because it's passing a path to‑Command
...
A Unix-like system passes the PowerShell script's absolute filename to the interpreter specified by the "shebang" as the first argument when we invoke the script as a command. In general, this can sometimes work for PowerShell 5 and below because PowerShell, by default, interprets the script filename as the command to execute.
However, we cannot rely on this behavior because when PowerShell's handles -Command
in this context, it re-interprets the filename as if it was typed at the prompt, so the path of a script that contains spaces or certain symbols will break the "command" that PowerShell sees as the argument. We also lose a bit of efficiency for the preliminary interpretation step.
When specifying the -File
parameter instead, PowerShell loads the script directly, so we can avoid the problems we experience with -Command
. Unfortunately, to use this option in the shebang line, we need to sacrifice the portability we gain by using the env utility described in the question because operating system program loaders usually allow only one argument to the program declared in the script for the interpreter.
For example, the following interpreter directive is invalid because it passes two arguments to the env
command (powershell
and -File
):
#!/usr/bin/env powershell -File
In an MSYS system (like Git Bash), on the other hand, a PowerShell script that contains the following directive (with the absolute path to PowerShell) executes as expected:
#!/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -File
...but we cannot directly execute the script on another system that doesn't follow the same filesystem convention.
This also doesn't fix the original problem in Cygwin. As described in the question, the path to the script itself isn't translated to a Windows-style path, so PowerShell cannot locate the file (even in version 6). I figured out a couple of workarounds, but neither provide a perfect solution.
The simplest approach just exploits the default behavior of PowerShell's -Command
parameter. After adding the Write-Foo.ps1
script to the environment's command search path (PATH
), we can invoke PowerShell with the script name, sans the extension:
$ powershell Write-Foo arg1 arg2 ...
As long as the script file itself doesn't contain spaces in the filename, this allows us to run the script from any working directory—no shebang needed. PowerShell uses a native routine to resolve the command from the PATH
, so we don't need to worry about spaces in the parent directories. We lose Bash's tab-completion for the command name, though.
To get the shebang to work in Cygwin, I needed to write a proxy script that converts the path style of the invoked script to a format that PowerShell understands. I called it pwsh (for portability with PS 6) and placed it in the PATH
:
#!/bin/sh if [ ! -f "$1" ]; then exec "$(command -v pwsh.exe || command -v powershell.exe)" "$@" exit $? fi script="$(cygpath -w "$1")" shift if command -v pwsh.exe > /dev/null; then exec pwsh.exe "$script" "$@" else exec powershell.exe -File "$script" "$@" fi
The script begins by checking the first argument. If it isn't a file, we just start PowerShell normally. Otherwise, the script translates the filename to a Windows-style path. This example falls back to powershell.exe if pwsh.exe from version 6 isn't available. Then we can use the following interpreter directive in the script...
#!/usr/bin/env pwsh
...and invoke a script directly:
$ Write-Foo.ps1 arg1 arg2 ...
For PowerShell versions before 6.0, the script can be extended to symlink or write out a temporary PowerShell script with a .ps1 extension if we want to create the originals without an extension.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With