-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add new check for error ignoring on command substitutions #2338
Comments
Yet another strike against If such a warning were given, I would like it to be gated on the presence of |
I couldn't agree more with @kurahaupo. |
Thanks to @DoxasticFox you can now enable an optional check for this:
results in
|
This is awesome! |
For reference, this is @DoxasticFox's PR: #2320 |
Using this feature, I realized there are: [[ $(command -v curl) ]] and my_function "$(cat <<EOF
my
multiline
parameter
EOF
)" Which are two cases very known, and return code on them is very likely not to mean anything. Should this rule have exceptions like these above? |
@felipecrs I think this: command -v curl and my_function "my
multiline
parameter
" should have similar effects. Why not these? |
For the first, you are right. Btw the drop-in replacement would be For the second, there are multiple benefits of using here-docs, such as 1. better readability in the code, 2. allow to be indented with |
Ah, I didn't understand the heredoc was the important part. Well, #2320 assumes that certain commands' exit codes can be safely masked. We could add In the meanwhile, another alternative is this: arg="$(cat <<EOF
my
multiline
parameter
EOF
)"
my_function "$arg" It's a bit of shame to have to introduce a variable to deal with this case though, I'll admit. But it wouldn't be the only case where using |
That's right. Thanks for the rationale. Perhaps an alternative solution would be to allow |
You can use |
Btw I created the issue for discussing: #2359 |
Thanks a lot, it works! |
Is enabling this optional rule necessary if you run your (bash) script with |
I don't get it. What is the relationship between this and -o pipefail? |
For context - I run my scripts with For example, I scanned my existing code: example 1
In my understanding enabling pipefail will not cause the errors to be swallowed because the entire "pipeline" will fail as soon as the first step fails. example 2Similarly here - if one step fails, the pipeline will fail and it would use the
Scanned section in question for example 2: # grep returns code 0 with no results
if ! jobs_in_queue=$(REDACTED status | tail --lines +2 | grep --count --invert-match "$excluded_strings"); then
if [ "$jobs_in_queue" == 0 ]; then
REDACTED "Processed all pending jobs."
break
fi
REDACTED "REDACTED: could not retrieve pending jobs." "$jobs_in_queue"
fi |
@GhostLyrics |
This is sadly a badly over-used idiom.
Multiline strings are valid, so if it's simple just use:
If you have complex embedded quoting, it would probably make the code clearer if the value is in a variable anyway:
Either way, the relevant exit status is more obvious to ShellCheck. |
For new checks and feature suggestions
Suggestion
Given the following script:
It would normally work, but
curl
could actually fail (such as with command not found). So, for the sake of the example, I will force it to fail by usingcurl2
.Examples:
With
curl2
:The status code of the latter example is 0 even if the command failed. This is misleading. ShellCheck could advise to be written like so instead:
References twpayne/chezmoi#1466 (comment)
The text was updated successfully, but these errors were encountered: