In shell, the string literal "\a" means the two character string 0x5C
0x61, i.e. a backslash followed by lower-case a.
$ echo "\a" | hd
00000000 5c 61 0a |\a.|
00000003
I guess you expected it to mean 0x07? If so then the mismatch between >expectation and reality seems like justification for the shellcheck
warning.
In fact, I'd expect something like:
printf "\\a"
to print a literal backslash followed by a literal a. This doesn't happen, >> which also surprises ms.
I don't know if I'd have predicted it cold, but it's consistent with
the documentation for printf(1).
I ran shellcheck on one of my bash scripts and got the output shown below:
*) printf "\a"
^-- SC1117: Backslash is literal in "\a". Prefer explicit escaping: "\\a".
This happens for every instance like this - where I have something like \a
or \n or \r or whatever in a string for things like echo or printf.
This surprises me, b/c the above is perfectly standard and normal. And
works perfectly well, so there clearly is nothing to change or fix here.
In fact, I'd expect something like:
printf "\\a"
to print a literal backslash followed by a literal a. This doesn't happen, which also surprises ms.
I ran shellcheck on one of my bash scripts and got the output shown below:
*) printf "\a"
^-- SC1117: Backslash is literal in "\a". Prefer explicit escaping: "\\a".
This happens for every instance like this - where I have something like \a
or \n or \r or whatever in a string for things like echo or printf.
This surprises me, b/c the above is perfectly standard and normal. And
works perfectly well, so there clearly is nothing to change or fix here.
In fact, I'd expect something like:
printf "\\a"
to print a literal backslash followed by a literal a. This doesn't happen, which also surprises ms.
Can someone explain what is going on here and why shellcheck complains?
(Yes, I know shellcheck complains about lots of stuff - most of it bogus - but this one I find curious...)
According to POSIX, a backslash inside double-quotes escapes the following:
$ ` " \ <newline>
If it isn't followed by one of those, it is treated as a literal backslash.
So in:
printf "\\a"
the first \ escapes the second and printf is passed \a
whereas in:
printf "\a"
the \ is treated as literal and printf is passed \a
So, the question is: Why does shellcheck complain?
That's the point of my starting this thread.
(Yes, I know shellcheck complains about lots of stuff -
most of it bogus - but this one I find curious...)
I ran shellcheck on one of my bash scripts and got the output shown below:
*) printf "\a"
^-- SC1117: Backslash is literal in "\a". Prefer explicit escaping: "\\a".
This happens for every instance like this - where I have something like \a
or \n or \r or whatever in a string for things like echo or printf.
This surprises me, b/c the above is perfectly standard and normal. And
works perfectly well, so there clearly is nothing to change or fix here.
In fact, I'd expect something like:
printf "\\a"
to print a literal backslash followed by a literal a. This doesn't happen, which also surprises ms.
Can someone explain what is going on here and why shellcheck
complains?
Richard Kettlewell <[email protected]d> wrote:
...
In shell, the string literal "\a" means the two character string 0x5C
0x61, i.e. a backslash followed by lower-case a.
$ echo "\a" | hd
00000000 5c 61 0a |\a.|
00000003
I guess you expected it to mean 0x07? If so then the mismatch between >>expectation and reality seems like justification for the shellcheck >>warning.
No, quite the opposite - in terms of what *I* expect. I know shell syntax
as well as (just about) anyone here, and I'm fully aware that the shell doesn't translate/interpret backslash sequences (unless you ask nicely for
it to do so with something like $'\n').
Maybe the author(s) of shellcheck think that *some* users might expect that.
Its a reasonable thing to think. For instance anyone more familiar with
C than shell would expect it.
In article <[email protected]>,
Geoff Clare <[email protected]> wrote:
...
According to POSIX, a backslash inside double-quotes escapes the following: >>
$ ` " \ <newline>
If it isn't followed by one of those, it is treated as a literal backslash. >>
So in:
printf "\\a"
the first \ escapes the second and printf is passed \a
whereas in:
printf "\a"
the \ is treated as literal and printf is passed \a
Exactly. So, you're saying that the two are exactly equivalent - the underlying command (printf in this case) sees the exact same thing in both cases.
So, the question is: Why does shellcheck complain?
That's the point of my starting this thread.
In article <[email protected]>,
Geoff Clare <[email protected]> wrote:
...
According to POSIX, a backslash inside double-quotes escapes the following: >>
$ ` " \ <newline>
If it isn't followed by one of those, it is treated as a literal backslash. >>
So in:
printf "\\a"
the first \ escapes the second and printf is passed \a
whereas in:
printf "\a"
the \ is treated as literal and printf is passed \a
Exactly. So, you're saying that the two are exactly equivalent - the underlying command (printf in this case) sees the exact same thing in both cases.
So, the question is: Why does shellcheck complain?
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 714 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 136:12:17 |
| Calls: | 12,087 |
| Files: | 14,997 |
| Messages: | 6,517,374 |