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

[SC2046] false positive from return $([ "$(tput colors)" -ge "8" ]) #3119

Open
SwuduSusuwu opened this issue Jan 11, 2025 · 3 comments
Open

Comments

@SwuduSusuwu
Copy link

SwuduSusuwu commented Jan 11, 2025

For bugs

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

#!/bin/sh
SUSUWU_HAS_STANDARD_CONSOLE() ( #/* Usage: `if SUSUWU_HAS_STANDARD_CONSOLE; then
$(SUSUWU_SH_USE "${SUSUWU_SH_<color>}" "<message>")` */
    if command -v "tput" >/dev/null; then #if installed `ncurses-utils`
        return $([ "$(tput colors)" -ge "8" ]) #standard console
    fi
    for CONSOLE in "xterm" "xterm-color" "xterm-256color" "screen" "screen-color" "screen-256color" "tmux" "tmux-color" "tmux-256color" "linux" "rxvt" "rxvt-unicode"; do
    if [ "${TERM}" = "${CONSOLE}" ]; then
         return 0 #standard console
    fi
    done
    return 1
)

(from SwuduSusuwu/SubStack@5f57561#diff-34e56429da29b8a769483ffcd6eb3c6089937cdc157438b119ae25d2b06750beR59-R69).

Here's what shellcheck currently says:

Quote this to prevent word splitting.

Here's what I wanted or expected to see:

How to add more quotes, or "Passed".

return $("[ "$(tput colors)" -ge "8" ]") is a syntax error (outputs [ "256" -ge "8" ]: command not found), and return "$([ "$(tput colors)" -ge "8" ])" is a syntax error (outputs No command found), so do not know how to add more quotes.

@brother
Copy link
Collaborator

brother commented Jan 11, 2025

I am not sure that code does what you want it to do but the correct quoting would be the complete subshell (second line in your report).

But let's break it down to try to understand what we have.
return $([ "$(tput colors)" -ge "8" ])

The return can be dropped first, we want to control the flow in the function and end on a exit code.

Starting inside the square brackets we have a subshell excuting tput colors. It will give us a exit code to work with.
That will be compared as being either greater than or equal to 8. That comparison will leave us with either 0 or 1.

brother ~$ [ "256" -ge "8" ]
brother ~$ echo $?
0
brother ~$ [ "8" -ge "8" ]
brother ~$ echo $?
0
brother ~$ [ "1" -ge "8" ]
brother ~$ echo $?
1

The square bracket comparison will then be passed up a step to the subshell and executed. In most cases I would assume that there are no command 0 nor 1 and thus you'll have "command not found".

I think shellcheck does suggest the correct quoting in this case. I am doubting that the code presented is constructed correct.

Maybe

        if [ "$(tput colors)" -ge "8" ]; then
            return 0 #standard console
        fi
        return 1

@SwuduSusuwu
Copy link
Author

SwuduSusuwu commented Jan 11, 2025

    if [ "$(tput colors)" -ge "8" ]; then
      return 0 #standard console
    fi
    return 1

is a workaround. Too bad that is 4 lines instead of 1 (such as return $([ "$(tput colors)" -ge "8" ])) to do this.

@SwuduSusuwu
Copy link
Author

SwuduSusuwu commented Jan 11, 2025

Workaround:

test "$(tput colors)" -ge "8" 
return $?

reduces those 4 lines to 2.

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

2 participants