010-100 · Question #69
Which character starts a comment line in a shell script file?
The correct answer is C. #. In shell scripts, the hash character (#) begins a comment line, causing the shell to ignore everything following it on that line.
Question
Which character starts a comment line in a shell script file?
Options
- A;
- B
- C
- D/
How the community answered
(31 responses)- A3% (1)
- B3% (1)
- C84% (26)
- D10% (3)
Why each option
In shell scripts, the hash character (#) begins a comment line, causing the shell to ignore everything following it on that line.
A semicolon is a command separator used to chain multiple commands on one line, not a comment character.
An asterisk is a glob wildcard that matches any string of characters in filename expansion, not a comment delimiter.
The # character is the standard comment delimiter in POSIX shell scripts and all common shells (bash, sh, zsh, etc.). Any text following # on a line is treated as a comment and not executed. The special case #! (shebang) on line one is interpreted by the kernel, not the shell itself.
A forward slash is used as a directory path separator and in constructs like command substitution or regex, not as a comment character.
Concept tested: Shell script comment syntax using hash character
Source: https://www.gnu.org/software/bash/manual/bash.html#Comments
Topics
Community Discussion
7C is your answer. In shell scripting, the hash sign tells the interpreter to ignore everything that follows on that line, which is how you write comments. The very first line of most shell scripts is actually a special use of this character, the shebang like #!/bin/bash, where the hash plus exclamation mark together signal the OS which interpreter to use. The other options do not start comments in any standard shell, semicolons separate commands, asterisks are wildcards, and forward slashes show up in paths and division, none of them comment anything out.
I kept second-guessing this one but landed on A, the semicolon, because in shell scripting the semicolon is used as a command separator and control character, so it made sense to me that the shell would treat it as a line-level syntax marker for comments too. The other options just never show up as special line starters in any script I have seen.
Toby, the comment character in shell scripting is actually the pound sign (C), not the semicolon. The semicolon does separate commands on a single line, but the shell uses # at the start of a line (or anywhere, really) to mark everything after it as a comment that gets ignored by the interpreter.
I put A, the semicolon, because in scripting I kept seeing semicolons used to separate or end statements, and I figured that same symbol doing a "stop here" job would also signal to the interpreter to stop and skip a line, which is basically what a comment does.
The semicolon is a statement separator or terminator in languages like Bash and PHP, not a comment marker. In most scripting contexts, the hash symbol, which is C, is what tells the interpreter to ignore everything that follows it on that line.
Pretty sure this is A, the semicolon. My senior had me look at a script last week and she pointed out how you chain commands together with semicolons, and I remember her saying something about how certain shells treat that symbol as a separator that can also mark off lines you do not want to run. I could be mixing things up but that is the one that stuck with me, so I am going with A on this one.
Yusuf, your senior was showing you command chaining, but the symbol that comments out the rest of a line in bash is the pound sign ( # ), which is option C. The semicolon just separates two commands that both run, it does not suppress anything.