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?

  • AFAIR, pwsh prior to version 6.0 did not support the concept of shebang. – fpmurphy Mar 25 '21 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 '22 at 15:45

3 Answers3

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.

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.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • Does that mean that you recommend still having a shebang? and only do so for unix and linux machines? – Abel Melquiades Callejo Mar 25 '21 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 – Chris Davies Mar 25 '21 at 17:18
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.

Andy
  • 202
  • 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 – Chris Davies Aug 11 '21 at 17:45