Comments, syntax errors and help
Write useful comments
A comment explains code to people who read it. In ordinary noninteractive Bash code, an unquoted # that begins a word starts a comment lasting to the end of the line. In interactive Bash this behavior is normally enabled but can be configured. A quoted # is ordinary text. A shebang is a special first-line convention for direct execution, not an ordinary documentation comment in that context.
# Show the report label.printf '%s\n' 'Report #1' # Keep the label short.Expected output when executed:
Report #1Only the command is executed. The #1 within the quoted argument is printed; the explanatory text after the unquoted # is ignored. Comments should explain a purpose or constraint rather than merely repeat the command name.
Check syntax without running the script
A syntax error means Bash cannot parse part of the code according to its grammar. Create a fresh file named broken.sh in the lab folder containing this deliberately invalid line. Do not paste the unfinished line directly into an interactive prompt: the shell may wait for the closing quote.
Standard output is empty; an error diagnostic appears on stderr and the exit status is nonzero.
Bash reports an unmatched quote or unexpected end of file. Exact wording and line numbers can vary by version. Add the closing quote and save the corrected line shown below as broken.sh, then run the check again.
Expected output when executed:
HelloExpected output when executed:
HelloA successful syntax check normally prints nothing. It does not execute the script’s commands. Here && runs the file only if the syntax check succeeds. Passing bash -n does not prove that dependencies or input files exist, or that the program produces the desired result. Those require execution tests.
Read built-in documentation
Expected output excerpt (full text varies by version):
printf:The help output includes usage and explanations of arguments. The exact text is version-dependent. For external tools, consult their own documentation, such as man pages when available; help is primarily for Bash built-ins and shell constructs.
Practice
Create a small script with a useful comment and a quoted hash sign. Remove a closing quote, use bash -n to detect the problem, repair it, and check again before running.
Next lesson: Read a command exit status