2

Shell scripts are better when having a shebang like so:

#!/bin/bash
echo "The shebang above this line works like a typical a comment but with a purpose"

PowerShell scripts are also shell scripts. So it should be okay to do like so:

#!/usr/local/bin/pwsh
echo "The shebang above this line works like a typical a comment but with a purpose"

It seems to does not make sense if the script was intended for Windows machines but for Linux and Mac machines they seem to make sense.

Do you think it would be best for PowerShell scripts to also have a shebang?

2
  • AFAIR, pwsh prior to version 6.0 did not support the concept of shebang.
    – fpmurphy
    Mar 25, 2021 at 16:38
  • @fpmurphy the first version of pwsh is 6.0. It's also the first version to run on non-Windows platforms. Older version are powershell and runs on Windows only which has no idea of shebang
    – phuclv
    May 22, 2022 at 15:45

3 Answers 3

3

When you run an executable script on UNIX/Linux derived systems, the kernel (or sometimes the shell) looks at the first line for a #! (shebang) marker that indicates the name of the interpreter to use.

If you don't have a shebang then there is no clear information about the method by which the script should be executed, and it's up to the user to specify an interpreter explcitly.

For example,

bash somescript         # user calls bash explicitly to run the script

vs

chmod a+x somescript    # make executable
head -n1 somescript     # show the first line
#!/bin/bash

./somescript            # the script itself defines bash to execute it

The same can apply for any interpreter, not just a standard shell. So you can have #!/bin/awk, #!/usr/bin/perl, etc.

Similarly there is no reason why you shouldn't have #!/usr/local/bin/pwsh provided that the interpreter can execute code read from stdin. It's what I'd aim to do.

2
  • Does that mean that you recommend still having a shebang? and only do so for unix and linux machines? Mar 25, 2021 at 16:12
  • 1
    I would recommend a #! shebang for any script on a UNIX/Linux machine that could benefit from it. Also, note that although the concept isn't implemented for Windows powershell, there's nothing stopping you using it there, because the # character introduces a comment line Mar 25, 2021 at 17:18
3

PowerShell scripts are primarily run under Windows and this OS pays attention to the file extension of all files except for .exe, .com, .cmd, .bat which are executed directly. Windows also doesn't have the concept of executable files - the so called "executable" bit in Unix OSes.

Linux on the other hand uses the shebang for files marked as executable to find out which interpreter to use, so the shebang becomes crucial.

Do you think it would be best for PowerShell scripts to also have a shebang?

For Windows machines it makes zero sense, for Linux machines it perfectly makes sense. In the end it's up to you to decide. Adding the PS shebang won't hurt, so you might add it to all your scripts, only I didn't know people use PS under Linux.

2
  • Does that mean that you do not recommend having a shebang? Only for windows machines? or even for unix and linux machines? Mar 25, 2021 at 16:13
  • 1
    For Windows machines it makes zero sense, for Linux machines it perfectly makes sense. Mar 25, 2021 at 18:23
1

Generally, you want your shebang to start with #!/usr/bin/env because the location of env is always in /usr/bin/ on all x-nixes. Unlike env, bash can sometimes be in /usr/bin, or /bin or both (depending on the OS). /usr/bin/env will search your path to find the right executable and makes your script far more portable.

#!/usr/bin/env pwsh

Would find pwsh on your path, provided that you are not root, as root often does not include /usr/local/bin on the path

The exception to this is when you are dealing with programs that have multiple environments (like a python virtualenv), where the shebang is used to call a specific environment of python. But virutalenvs are never portable, so sacrificing portability here is consistent.

1
  • There are definitely pros and cons with using #!/usr/bin/env. In many cases I want my script to run regardless of the user's PATH. Your version requires it to be correct. It seems YMMV rather than a hard and fast rule Aug 11, 2021 at 17:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .