shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

Bash 5.3's alternate command substitution supports `local` variables, but SC2168 is shown

Open jansorg opened this issue 4 months ago • 0 comments

For bugs with existing features

  • Rule Id SC2168 ('local' is only valid in functions)
  • ShellCheck 0.11.0
  • [x] The rule's wiki page does not already cover this (e.g. https://shellcheck.net/wiki/SC2086)
  • [x] I tried on https://www.shellcheck.net/ and verified that this is still a problem on the latest commit

Here's a snippet or screenshot that shows the problem:

#!/usr/bin/env bash

myVar=global
echo "${
  myVar=global_too
  local myVar=local_value
  echo "after local command: $myVar"
}"

echo "In global scope: $myVar"

Here's what shellcheck currently says:

In file.sh line 6:
  local myVar=local_value
  ^---^ SC2168 (error): 'local' is only valid in functions.

Here's what I wanted or expected to see:

According to man bash of Bash 5.3 `local' is supported for the alternate command substitution syntax:

This type of command substitution superficially resembles executing an unnamed shell function:
local variables are created as when a shell function is executing, and the return builtin forces command to complete; however, the rest of the execution environment, including the positional parameters, is shared with the caller.

The output of the script above is:

after local command: local_value
In global scope: global_too

jansorg avatar Aug 28 '25 10:08 jansorg