shellcheck generated the following warning

SC2155: Declare and assign separately to avoid masking return

For this line of code

local key_value=$(echo "$current_line" | mawk '/.+=.+/ {print $1 }')

What does "masking return values" mean, and how does it pertain to the aforementioned warning?

New contributor
just-in-time_learning is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

When you declare a variable as either local or exported that in itself is a command that will return success or not.

$ var=$(false)
$ echo $?
$ export var=$(false)
$ echo $?

So if you wanted to act on the return value of your command (echo "$current_line" | mawk '/.+=.+/ {print $1 }'), you would be unable to since it's going to exit with 0 as long as the local declaration succeeds (which is almost always will).

In order to avoid this it suggests declaring separately and then assigning:

local key_value
key_value=$(echo "$current_line" | mawk '/.+=.+/ {print $1 }')

This is a shellcheck rule I frequently ignore and IMO is safe to ignore as long as you know you aren't trying to act on the return value of that variable declaration.

You can ignore it by adding the following to the top of your script (Below the hashbang of course):

# shellcheck disable=SC2155

Your Answer

just-in-time_learning is a new contributor. Be nice, and check out our Code of Conduct.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.