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

SC2059: What about when escape codes (e.g. ANSI colors), not printf specifiers, are in the variable? #3093

Open
2 of 4 tasks
techflashYT opened this issue Dec 2, 2024 · 2 comments

Comments

@techflashYT
Copy link

For bugs

For new checks and feature suggestions

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

#!/bin/sh

somevar="\e[1;31mSome red text\e[0m"

# this code functions correctly, but shellcheck produces SC2059
printf "$somevar\n"

# this code does NOT function correctly (printing a literal "\e[..."
# However, shellcheck doesn't complain.
printf "%s\n" "$somevar"

Here's what shellcheck currently says:

In a.sh line 6:
printf "$somevar\n"
       ^----------^ SC2059 (info): Don't use variables in the printf format string. Use printf '..%s..' "$foo".

For more information:
  https://www.shellcheck.net/wiki/SC2059 -- Don't use variables in the printf...

Here's what I wanted or expected to see:

No error about the line 6, as putting it outside of the format string, like in the latter example, does not produce the intended result.
OR
A different warning, explaining a better way to handle this case.
OR
Information about this case added to the SC2059 wiki page, which currently only specifies what to do in the case of there being a % in the string.

@wileyhy
Copy link

wileyhy commented Dec 3, 2024 via email

@ale5000-git
Copy link

ale5000-git commented Dec 14, 2024

This is safer:

somevar="Some red text"
printf "\e[1;31m%s\e[0m\n" "$somevar"

It will make sure that if the $somevar variable is passed and contains unsafe characters they are printed as is and not interpreted.

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

3 participants