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

Warn about using grep outside conditionals when set -e is in effect #1165

Open
2 tasks done
baumanj opened this issue Mar 29, 2018 · 0 comments
Open
2 tasks done

Warn about using grep outside conditionals when set -e is in effect #1165

baumanj opened this issue Mar 29, 2018 · 0 comments

Comments

@baumanj
Copy link

baumanj commented Mar 29, 2018

For new checks and feature suggestions

Using grep in the context of set -e, it is not generally the intent to exit the script on non-match situations. When the grep is in the context of a conditional, this is avoided, but sometimes it's used to assign variables and the result is unexpected.

Real world example: this line caused habitat-sh/habitat#4834

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

#!/bin/sh

set -e

matches=$(grep foo bar)
echo "${matches:-no matches}"

Here's what shellcheck currently says:

Nothing

Here's what I wanted or expected to see:

In test.sh line 5:
matches=$(grep foo bar)
          ^-- SCXXXX: Consider 'grep ... || true' or else script will exit on no match due to set -e
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