Which shell executes scripts when there's no shebang line (#!/path/to/shell) at the beginning? I assume /bin/sh but I can't confirm.
The kernel refuses to execute such scripts and returns ENOEXEC, so the exact behavior depends on the program you run such a script from.
- bash 4.2.39 – uses itself
- busybox-ash 1.20.2 – uses itself
- dash 0.5.7 – runs /bin/sh
- fish 1.23.1 – complains about ENOEXEC, then blames the wrong file
- AT&T ksh 93u+2012.08.01 – uses itself
- mksh R40f – runs /bin/sh
- pdksh 5.2.14 – runs /bin/sh
- sh-heirloom 050706 – uses itself
- tcsh 6.18.01 – runs /bin/sh
- zsh 5.0.0 – runs /bin/sh
- cmd.exe 5.1.2600 – looks at you funny.
In glibc, functions execv()
or execve()
just return ENOEXEC. But execvp()
hides this error code and automatically invokes /bin/sh. (This is documented in exec(3p).)
What is considered "best practices" in terms of writing shell scripts that will run on any platform? (ok, this is sort of open-ended)
Either stick to sh
and only POSIX-defined features, or just go full bash (which is widely available) and mention it in your requirements if distributing it.
(Now that I think of it, Perl – or perhaps Python – would be even more portable, not to mention having a better syntax.)
Always add the shebang line. If using bash or zsh, use #!/usr/bin/env bash
instead of hardcoding the shell's path. (However, the POSIX shell is guaranteed to be at /bin/sh
, so skip env
in that case.)
(Unfortunately, even /bin/sh
is not always the same. The GNU autoconf program has to deal with many different quirks.)
Is it possible to write a script that tries to use zsh and falls back to bash if zsh is not available? I've tried putting two shebang lines, like below, but it just errors with bad interpreter: /bin/zsh: no such file or directory out if I try it on a machine without zsh.
There can only be one shebang line; everything after the newline character isn't even read by the kernel, and treated as a comment by shells.
It's possible to write a script that runs as #!/bin/sh
, checks which shell is available, and runs exec zsh "$0" "$@"
or exec bash "$0" "$@"
depending on the result. However, the syntax used by bash and zsh is so different in various places that I would not recommend doing this for your own sanity.
I decided to use the pure theme for zsh. My scripts work as normal, they just run via zsh now. Here is what my ~/.zshrc file looks like:
# PATHS AND ALIASES
# searches this directory for executables first
export PATH="/usr/local/bin:$PATH"
# jenv
export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"
# rbenv
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
# pyenv
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
# nodenv
export PATH="$HOME/.nodenv/bin:$PATH"
eval "$(nodenv init -)"
# node-build-definitions
export NODE_BUILD_DEFINITIONS="/usr/local/opt/node-build-update-defs/share/node-build"
# firevault memory security
alias sleepsafe='sudo pmset -a destroyfvkeyonstandby 1 hibernatemode 25 standby 0 standbydelay 0'
alias sleepdefault='sudo pmset -a destroyfvkeyonstandby 0 hibernatemode 3 standby 1 standbydelay 10800'
# enable / disable captive portal
alias disablecaptiveportal='sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.captive.control Active -bool false'
alias enablecaptiveportal='sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.captive.control Active -bool true'
# CLI SETTINGS
# enable the default zsh completions
autoload -Uz compinit && compinit
# set colors for displaying directories and files when the ls command is used
export LSCOLORS='GxFxCxDxBxegedabagaced'
export CLICOLOR=1
# theme
fpath+=("$HOME/.zsh/pure")
autoload -U promptinit && promptinit
prompt pure
# change the path color
zstyle :prompt:pure:path color white
Best Answer
This is called process substitution.
The
<(list)
syntax is supported by both,bash
andzsh
. It provides a way to pass the output of a command (list
) to another command when using a pipe (|
) is not possible. For example when a command just does not support input fromSTDIN
or you need the output of multiple commands:<(list)
connects the output oflist
with a file in/dev/fd
, if supported by the system, otherwise a named pipe (FIFO) is used (which also depends on support by the system; neither manual says what happens if both mechanisms are not supported, presumably it aborts with an error). The name of the file is then passed as argument on the command line.zsh
additionally supports=(list)
as possible replacement for<(list)
. With=(list)
a temporary file is used instead of file in/dev/fd
or a FIFO. It can be used as a replacement for<(list)
if the program needs to lseek in the output.According to the ZSH manual there might also be other issues with how
<(list)
works: