• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

Bash Comments

Thumbnail for Prianka SubrahmanyamPrianka Subrahmanyam

Prianka Subrahmanyam

Software Engineer, Modern Treasury

Published: 8/13/2024

About Terminus

When to use Bash comments

Just like comments are used in other programming languages, you can also use them throughout your shell scripts to make them more readable. For example, if you’ve written a script for others to use, comments help explain the purpose of certain lines. They can also help you, the developer, quickly remember your thought process from when you wrote the script.

Comments should be used to complement clean, organized Bash scripts. They should only be used to explain less obvious parts of the code. For example, most developers reading an echo statement will know how it works, but a user-defined function may be harder to immediately understand, so a shell script comment would be helpful there.

Single-line comments

To create a single-line comment in a Bash script, begin the comment with a hash (#) symbol. Bash ignores everything after the hash, so even if you include code snippets in comments, they won’t be executed.

A comment can be on a line alone:

# This is a comment
$ ls path/to/directory

Run in Warp

A comment can also be on the same line as script code:

$ ls path/to/directory  # This is also a valid  way to write comments
Run in Warp

Multiline/Block comments

Unfortunately, there is no built-in way to write multi-line comments in Bash. But here are some workarounds.

Multiple single-line comments

You can use multiple single-line comments to make up a larger overall comment:

# The following code uses the ls command
# The ls command lists the files in a directory.
$ ls path/to/directory

Run in Warp

Using a colon and single quotes

The simplest workaround is using the colon and single quote characters:

: ' 
This comment can be
many lines long
Within this block
'

Run in Warp

Arbitrarily-named variables

You can also assign an arbitrarily named variable to a multi-line string to serve the same purpose as a comment - it’s still a block of text that’s recognizable to anyone using your shell script. This is useful if you want to give the comment a name.

example_comment=' 
This is technically a variable.
But it serves the same purpose as a comment.
'

Run in Warp

HereDoc redirection

You can utilize the HereDoc redirection mechanism as a workaround to write multi-line comments in Bash. Usually, HereDoc is used when you need to pass more than one input line to a command, but if you don’t specify a command, the lines have no effect and can serve as a way to write “comments” spanning multiple lines.

<< 'COMMENT-EXAMPLE' 
  This is a multi-line comment hack.
  You can give the HereDoc any title you want.
  Here we used COMMENT-EXAMPLE.
COMMENT-EXAMPLE

Run in Warp

Keep in mind that commenting through HereDoc is not officially supported by Bash, so it’s best practice to stick to hash symbol comments, or the above.

Read more about commenting in bash in the official bash man pages.

Written by

Thumbnail for Prianka SubrahmanyamPrianka Subrahmanyam

Prianka Subrahmanyam

Software Engineer, Modern Treasury

Filed Under

Related Articles

Bash If Statement

Learn how to use the if statement in Bash to compare multiple values and expressions.

Bash
Thumbnail for Gabriel ManricksGabriel Manricks

Bash While Loop

Learn how to use and control the while loop in Bash to repeat instructions, and read from the standard input, files, arrays, and more.

Bash

POST JSON Data With Curl

How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.

BashUnixLinux
Thumbnail for Neeran GulNeeran Gul

Use Cookies With cURL

Learn how to store and send cookies using files, hard-coded values, environment variables with cURL.

Bash

Loop Through Files in Directory in Bash

Learn how to iterate over files in a directory linearly and recursively using Bash and Python.

BashPython
Thumbnail for Razvan LudosanuRazvan Ludosanu

How To Use sudo su

A quick overview of using sudo su

LinuxUnixBash
Thumbnail for Razvan LudosanuRazvan Ludosanu

Generate, Sign, and View a CSR With OpenSSL

Learn how to generate, self-sign, and verify certificate signing requests with `openssl`.

BashLinuxUnix
Thumbnail for Razvan LudosanuRazvan Ludosanu

How to use sudo rm -rf safely

We'll help you understand its components

BashLinuxUnix
Thumbnail for Neeran GulNeeran Gul

How to run chmod recursively

Using -R is probably not what you want

LinuxBashUnix
Thumbnail for Brett TerpstraBrett Terpstra

Run Bash Shell In Docker

Start an interactive shell in Docker container

DockerBash
Thumbnail for Razvan LudosanuRazvan Ludosanu

Curl Post Request

Use cURL to send data to a server

BashUnixLinux
Thumbnail for Zev StravitzZev Stravitz

Reading User Input

Via command line arguments and prompting users for input

BashLinuxUnix
Thumbnail for Amit JotwaniAmit Jotwani

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Request demo
Thumbnail for null