shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

Support multiple shells

Open ljharb opened this issue 9 years ago • 6 comments

I've got a script that attempts to be compatible with sh, dash, bash, zsh, and ksh. I'd love to specify multiple shells, so I don't have to manually merge and dedupe all the errors across the shells.

ljharb avatar May 10 '16 08:05 ljharb

Do you have guarded shell specific code in this script, e.g. if [ -n "$BASH_VERSION" ]; then bash-specific-code-here; fi?

If not, just setting the shell to sh should be enough. It's the lowest common denominator. Anything that passes cleanly for sh is unlikely to have any useful output for any of the other shells.

koalaman avatar May 10 '16 16:05 koalaman

Yes, I do. However, I've also gotten dash warnings that I don't get in sh, for things like local and type.

ljharb avatar May 10 '16 16:05 ljharb

What I did was just put my Bash-specific stuff into a separate file. So like:

# shellcheck shell=sh

... # POSIX-compatible stuff for all shells

if [ -n "$BASH_VERSION" ]; then . features.bash; fi

I guess doing so helps me keep things neat. :)


Originally I was hoping to do something like:

# shellcheck shell=sh

... # POSIX-compatible stuff for all shells

# shellcheck shell=bash
if [ -n "$BASH_VERSION" ]; then . features.bash; fi

Where everything below our shellcheck directives can be checked against their corresponding shell, but it looks like shellcheck only looks for a directive in the first block of comments from the top of the file.

andreykaipov avatar Mar 19 '21 17:03 andreykaipov

Unfortunately that’s not an option for my project; as it needs to be distributed, from git, as a single file.

ljharb avatar Mar 19 '21 18:03 ljharb

Any possibility that a shellcheck "shell" directive could be a comma-separated list?

ljharb avatar May 04 '21 04:05 ljharb

This is a nice feature request. For example, my ~/.profile is supposed to be compatible with sh, bash, and zsh, since I source it in both Bash and ZSH initialization.

felipecrs avatar Jun 04 '22 14:06 felipecrs