Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SC2154 from strict bash mode example using trap #2980

Open
3 of 4 tasks
peterjc opened this issue May 9, 2024 · 0 comments
Open
3 of 4 tasks

SC2154 from strict bash mode example using trap #2980

peterjc opened this issue May 9, 2024 · 0 comments

Comments

@peterjc
Copy link

peterjc commented May 9, 2024

For bugs

For new checks and feature suggestions

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

#!/bin/bash
set -uo pipefail
trap 's=$?; echo "$0: Error on line "$LINENO": $BASH_COMMAND"; exit $s' ERR
IFS=$'\n\t'

echo "Example of strict bash mode from Michael Daffin, see"
echo "https://olivergondza.github.io/2019/10/01/bash-strict-mode.html"

Here's what shellcheck currently says:

[Line 3:](javascript:setPosition(3, 6))
trap 's=$?; echo "$0: Error on line "$LINENO": $BASH_COMMAND"; exit $s' ERR
     ^-- [SC2154](https://www.shellcheck.net/wiki/SC2154) (warning): s is referenced but not assigned.

Here's what I wanted or expected to see:

No error, my understanding is this does define s. Currently https://www.shellcheck.net/wiki/SC2154 currently says:

ShellCheck intentionally does not attempt to figure out runtime or dynamic assignments like with source "$(date +%F).sh" or eval var=value. See SC2034 for an extended discussion of why this is the case.

Is using trap equivalent, and if so it that worth adding to the documentation?

Is the recommendation as per the SC2034 documentation to use a throw-away variable _ like so:

#!/bin/bash
set -uo pipefail
trap '_=$?; echo "$0: Error on line "$LINENO": $BASH_COMMAND"; exit $_' ERR
IFS=$'\n\t'

echo "Example of strict bash mode from Michael Daffin, see"
echo "https://olivergondza.github.io/2019/10/01/bash-strict-mode.html"

This passes shellcheck 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant