Comments in bash scripts
May 18, 2021 ‐ 1 min read
Comments in bash, or in other shells, start with a hashtag(#
). Everything after the hashtag is ignored, so you can have comments that span the entire line or use comments inline after a command.
# Create a file if it doesn't exist
if [[ ! -f $file ]]; then
touch $file # Create the file using the touch command
fi
Bash does not support multiline comments like many other languages. If you want comments that span multiple lines you should add a #
before each line.
# Create a file if it doesn't exist
# if [[ ! -f $file ]]; then
# touch $file
# fi
If you really want comments that span multiple lines you may misuse HEREDOC strings.
<<COMMENT
You may use HEREDOC to
write multiline comments
COMMENT
Pro tip! you can use comments in your terminal as well. This way you can store them in your shell history without having to run them for example.
$ # git rev-list --max-parents=0 HEAD | tail -n 1