From 8bb5e01401ade0962727d9001ba34cfbf10937d6 Mon Sep 17 00:00:00 2001 From: Claudio Bley Date: Fri, 12 Feb 2021 10:44:18 +0100 Subject: [PATCH] Allow `env` to have flags and variables in shebang The `env` command has a `-S,--split-string` option which enables having arguments for the command in a shebang. Also, one could use variable assignments for the command since `env` treats only the first word without a `=` character as the command to run. Fixes #2143. --- src/ShellCheck/Parser.hs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ShellCheck/Parser.hs b/src/ShellCheck/Parser.hs index 03865af..9f5208e 100644 --- a/src/ShellCheck/Parser.hs +++ b/src/ShellCheck/Parser.hs @@ -3190,6 +3190,7 @@ prop_readScript2 = isWarning readScript "#!/bin/bash\r\necho hello world\n" prop_readScript3 = isWarning readScript "#!/bin/bash\necho hello\xA0world" prop_readScript4 = isWarning readScript "#!/usr/bin/perl\nfoo=(" prop_readScript5 = isOk readScript "#!/bin/bash\n#This is an empty script\n\n" +prop_readScript6 = isOk readScript "#!/usr/bin/env -S X=FOO bash\n#This is an empty script\n\n" readScriptFile sourced = do start <- startSpan pos <- getPosition @@ -3231,13 +3232,14 @@ readScriptFile sourced = do where basename s = reverse . takeWhile (/= '/') . reverse $ s + skipFlags = dropWhile ("-" `isPrefixOf`) getShell sb = case words sb of [] -> "" [x] -> basename x - (first:second:_) -> + (first:args) -> if basename first == "env" - then second + then fromMaybe "" $ find (notElem '=') $ skipFlags args else basename first verifyShebang pos s = do