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 and $$ #1541

Open
phs75 opened this issue Apr 11, 2019 · 4 comments
Open

SC2059 and $$ #1541

phs75 opened this issue Apr 11, 2019 · 4 comments

Comments

@phs75
Copy link

phs75 commented Apr 11, 2019

The following code

printf "$$"
printf "$$%s" "foo"

triggers SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo" for the 1st line, but not for the second. How come?

I run ShellCheck version: 0.6.0

@koalaman
Copy link
Owner

This is intended for people who use printf as if it was echo, e.g. printf "Hello $USER, the time is $(date)\n", without realizing that a major point of printf is to separate the format string and values.

If you do specify a format string with values, you're obviously already aware of this, so ShellCheck assumes that your mixing of template arguments and variable expansions was intentional.

I haven't actually checked whether this is a valid assumption, so that's worth looking into.

@matthewpersico
Copy link

I’d say if only instances of %s are in the format use echo. Printf for formatting implies right justify or length limiting or printing as a number. If your not doing any of those, use rxho

@koalaman
Copy link
Owner

printf does behave more predictably with respect to backslashes and dashes though

@phs75
Copy link
Author

phs75 commented Apr 12, 2019

@koalaman Thanks for the explanation. I thought there was something special with $$. Should have done more tests and/or look at the code ...

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