🎓 A1Learner
🔍
0 of 47 lessons completed

Bash Comments

A comment is a note inside a script that Bash completely ignores. Comments explain the script to people without changing what runs.

A comment line prints nothing

The first line is a comment, so Bash skips it. Only the echo line below it prints.

# This line is ignored
echo "Only this prints"

A comment after code

The echo runs because it sits before the # sign. The note after the # sign never reaches the screen.

echo "Counted" # this part is a comment

Comments between real lines

Three comment lines sit between two echo lines. The output holds only the two real lines, in order.

# Start of the check
echo "First result"
# Middle note
# Another note
echo "Second result"

Example

# Daily backup script
# Prints the backup steps
# Author: the ops team
echo "Backup started" # opening step
echo "Files copied" # main step
echo "Backup finished"

Key points

  • Comment: a note in a script that Bash does not run.
  • A comment starts with the # character.
  • Everything after # on a line is ignored.
  • A trailing comment sits after real code on the same line.
  • The code before a trailing comment still runs normally.
  • A header block is a few comment lines at the top of a script.

In this example, the three opening comment lines form a header block and print nothing. The first echo prints its step and ignores its trailing comment. The second echo does the same. The last echo prints the closing line, so only three lines appear.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which line is a comment?