Skip to content

Recommend changing double quotes to single quotes wherever it is safe to do so #3511

Description

@kurahaupo

For new checks and feature suggestions

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

#!/bin/sh
echo "Hello World"

Here's what shellcheck currently says:

(nothing)

Here's what I wanted to see:

echo "Hello World"
     ^--- SC-XXXX: prefer single quotes unless quoting an expansion
Suggestion: echo 'Hello world'

Application

Identify all double-quoted strings, then:

  • exclude strings that contain a ' (because we don't want to recommend rewriting "say 'cheese'!" as `'say '\''cheese'\''!')
  • exclude strings that contain an expansion (in practice, just look for an unescaped ` , or an unescaped $ that is followed by a digit or a letter or an underscore or any of {, (, [ , @, #, $, *, ?).
  • exclude strings that are not just one quoted segment (e.g. "Foo Bar" would be reported but 'Foo'\ "Bar" would not.
  • change \" to just " in any suggested replacement

Rationale

If one consistently uses single quotes whenever double quotes are not required, then the presence of double quotes becomes an indication to the reader that they have to pay more attention as something "special" is happening. (This is similar to the rationale for preferring $var over ${var}. In both cases the avoidance of "consistency" is intentional and preferable, because it provides clearer signalling.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions