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

RFC/E: more simplification / dropping of unnecessary quotes #996

Open
scop opened this issue Apr 25, 2023 · 3 comments
Open

RFC/E: more simplification / dropping of unnecessary quotes #996

scop opened this issue Apr 25, 2023 · 3 comments

Comments

@scop
Copy link
Contributor

scop commented Apr 25, 2023

It seems there's room for some more removal of unnecessary quotes on simplify; to my knowledge word splitting does not occur:

  • within [[ ]] -- some quotes are removed in this context already, but not e.g from the right side of things like [[ $foo == "$bar" ]]
  • on assignment (= and +=)

I don't know offhand how this is for other shells besides bash, but thought I'd drop the suggestion here for comments already based just on the bash info.

@akinomyoga
Copy link

akinomyoga commented Apr 25, 2023

Similarly, in Bash, case $word in and here strings <<< $word are unnecessary in most cases. Some notes:

  • The quotes in the right-hand sides of ==, !=, and =~ in conditional commands [[ ]] should not be removed. The quote has special meaning in those contexts.
  • When a word on the right-hand side of assignments and [[ ... ]] contains $* or ${arr[*]}, they need to be quoted as "$*" or "${arr[*]}". Bash <= 4.2 has a bug that the joining character (the first character of IFS) is replaced by a whitespace .
  • Similarly, when a word in case and in here documents contain $* or ${arr[*]}, they need to be quoted. Bash <= 4.4 has a bug.
  • Needless to say, when the word contains literal shell special characters [[:space:]|&;<>\`"$], the quote for these characters cannot be removed. For example, var="a b c" cannot be unquoted to be var=a b c.

Edit: Bash <= 4.3 has a bug in here strings: koalaman/shellcheck#1009 (comment), so here strings need to be quoted as far as bash <= 4.3 is supported.

@scop
Copy link
Contributor Author

scop commented Apr 26, 2023

Thanks for the info. What I'm specifically thinking of are the simple "$foo" cases. Any known issues dropping the quotes from them in the mentioned contexts?

@akinomyoga
Copy link

akinomyoga commented Apr 26, 2023

For the simple case "$foo", I think there aren't any problems in the mentioned contexts at least in Bash. They can be always unquoted in

  • the right-hand sides of assignments (var=$foo),
  • the conditional commands (except for the right-hand sides of ==, !=, and =~) ([[ $foo == "$bar" ]]),
  • the target of the case statements (case $foo)
  • and the here documents (<<< $foo) edit: Bash <= 4.3 has a bug in here strings: $value in <<< $value doesn't need to be quoted koalaman/shellcheck#1009 (comment), so here strings need to be quoted as far as bash <= 4.3 is supported.

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