I've looked at these posts:
- How to put a line comment for a multi-line command
- Commenting in a Bash script inside a multiline command
but it doesn't answer conceptually why not.
For example:
#!/bin/bash
echo \
# comment
-n hello
on shellcheck.net yields:
-n hello ^-- SC2215 (warning): This flag is used as a command name. Bad line break or missing [ .. ]?
Reason I thought it would be okay:
- I had learned comments are ignored by programs (and so it should make no difference)
For example, a C
program:
#include <stdio.h>
int main() {
printf("hello%d\n",
// my comment
3);
}
Why is this fundamentally different in bash
and sh
?
How is it being interpreted that causes this (with comments not being completely ignored by the interpreter)?
My idea:
- the shell simply ignores lines with comments while parsing, but expects a continuation line after the backslash and for some reason (why?) breaks if a comment (which should be ignored) is there instead of looking at the next (uncommented) line
- C compiler never even sees comments because they are gone by compilation stage