[kwlug-disc] Why I Don't Use #!/bin/bash - Shebangs Explained! (Another one by Dave at YSAP)

John Sellens jsellens at syonex.com
Sun Aug 31 14:00:52 EDT 2025


I hope my notes might be helpful.

On Sun, 2025/08/31 01:27:46PM -0400, William Park via kwlug-disc <kwlug-disc at kwlug.org> wrote:
| Ah yes, I use ':' quite a lot at the top of script.
| 
|   - to set default if parameter is empty/null, eg.
|     : ${xxx:=default}

Some shells let you use : as an or in operators with variables.

Bourne shell (sh) and many others have a : command that
you can use as the first token on a line, which is equivalent
to the true command.  My dash(1) man page says

     :
     true   A null command that returns a 0 (true) exit value.



| On 2025-08-31 09:50, Jason Eckert wrote:
| > Somewhat related: If you've ever traced an ancient UNIX or Linux script,
| > you'd likely see a single colon on the first line instead of a shebang -
| > this is a shortcut to the true function and guarantees that the script
| > will run in the current shell, no matter what the calling shell is
| > (e.g., if you're in zsh and you run "bash script1.sh", it would still
| > run in zsh if the script started with a ":" line). It's one of those
| > things that are incredibly useful the few times you need it (and I've
| > used it many times over the years).

I suspect that's not actually accurate.  You're saying that if you pass
a script as a parameter to the bash command, bash will see the valid
(for bash) : command at the top of the script, and will replace itself
with zsh.

Perhaps if you run the script with ./script1.sh and the kernel doesn't
see a #! at the start or some other magic number, the kernel might
possibly run it with $SHELL.

On my machine, an executable text file that starts with a : command
gets run by /bin/sh (not my $SHELL):

    % echo $SHELL
    /bin/tcsh
    % cat /tmp/myscript.sh
    :
    ps x | grep script
    % /tmp/myscript.sh
    2364492 pts/11   S+     0:00 /bin/sh /tmp/myscript.sh
    2364494 pts/11   S+     0:00 grep script



Hope that's helpful - cheers

John


More information about the kwlug-disc mailing list