- Start a line with #.
- Bash ignores everything after # on that line.
#!/bin/bash
# This is a single-line comment
echo "Hello, World!" # Inline comment after a command- Can be placed after commands on the same line.
echo "Install nginx" # This prints the installation stepBash has no native multi-line comment syntax. Use one of these tricks:
- Using : (No-op)
: '
This is a multi-line comment
Spanning multiple lines
Ignored by Bash
'- Using <<COMMENT (Here Document)
<<COMMENT
This is another way to create
multi-line comments in Bash.
It will be ignored by the shell.
COMMENT# echo "This line will not run"
echo "This line will run"- Always use comments to explain complex logic.
- Keep comments short and clear.
- Use inline comments sparingly; prefer explanatory blocks above the code.
- Use docstrings for functions (not enforced by Bash, but a convention):
my_function() {
# This function prints a greeting message
echo "Hello, $1"
}- You can print sections or purpose headers in scripts:
echo "
########### Script Starting ###########
Purpose: Install NGINX
######################################
"