How to Comment in Bash

Monday, Aug 12, 2024 | 4 minutes read | Update at Monday, Aug 12, 2024

@ İsmail Baydan

In Bash, comments are created using the # symbol. Anything following # on the same line is considered a comment and is ignored by the shell. This means the # sign prevents commands in the start after its occurence. Here are 20 examples of how to use comments in Bash scripts: Bash comments are very usefull to describe code. Add some notes about code or something else.

  1. Single-line comment: The most basic usage for the bash comment is like below. We can call is single line comment. It will not executed as bash script.

    # This is a single-line comment
    
  2. Comment at the end of a line: We can add comment after some bash code. The characters after # interpreted as comment and not executed by Bash but previous characters will be executed as code. In the following example Hello World! will be display on the shell by comment part will be displayed.

    echo "Hello, World!" # This comment explains the echo command
    
  3. Commenting out a line of code: We can comment existing code line. This will make the code passive and the code will not executed. This can be useful to test code and then disable without deleting it.

    # echo "This line is commented out and will not run"
    
  4. Describing a variable: Bash provides variables and in a long bash script we may use lots of variables. We can use the comment in order to describe and detail the variable in order to provide information for future review.

    # The following variable stores the user's name
    USERNAME="Alice"
    
  5. Inline comment within a command: We can create inline comment like below inside a code line after the code.

    echo "Starting process..." # Begin the process
    
  6. Commenting a block of code: We can comment a block of code which is similar to the variable description.

    # The following block of code is responsible for setting up the environment
    export PATH=/usr/local/bin:$PATH
    export LANG=en_US.UTF-8
    
  7. Commenting out a function: We can comment a function by using the # and this will passive the function. We should use # from definition of the function to the end of the function.

    # This function is temporarily disabled
    # my_function() {
    #    echo "This won't run"
    # }
    
  8. Using comments to debug: We can use comment in order to pasive or active the debug code lines. In the production we can comment the debug code which will make the bash script execution clear.

    # echo "Debug: Variable value is $VAR"
    
  9. Describing a loop: We can use the bash comment in order to desribe loops. We can put the comment inside loop of before loop.

    # Loop through all files in the directory
    for file in *; do
        echo "$file"
    done
    
  10. Commenting command options: We can comment executed command options in order to remember the usage.

    # Using -l option to list files in long format
    ls -l
    
  11. Adding a comment to explain a conditional statement: Conditional statements like if can be described by using bash comments.

    # Check if the file exists
    if [ -f "$file" ]; then
        echo "File exists"
    fi
    
  12. Commenting script metadata: Another useful usage is commenting a bash script by adding meta data. We can add Author , Date , Description etc.

    # Author: John Doe
    # Date: 2024-08-10
    # Description: This script backs up user data
    
  13. Describing a function’s purpose: We can use the bash comment in order to descrive function purpose. We can give detailed information about the function like usage, parameters, return value etc.

    # This function adds two numbers
    add_numbers() {
        echo $(($1 + $2))
    }
    
  14. Commenting out multiple lines: We can use the bash comment as multiple lines.

    # The following lines are commented out for testing
    # echo "Line 1"
    # echo "Line 2"
    
  15. Commenting a script header: We can comment the script header for brief information about the script file.

    #!/bin/bash
    # This is a sample script to demonstrate comments in Bash
    
  16. Explaining an exit code: We can explain the return value or exit code with the comment.

    # Exit with code 0 (success)
    exit 0
    
  17. Describing command substitution: We can describe the command substitution with the comment.

    # Get the current date and time
    CURRENT_TIME=$(date)
    
  18. Commenting on the use of special variables:

    # $0 contains the name of the script
    # $1 contains the first argument passed to the script
    echo "Script name: $0"
    echo "First argument: $1"
    
  19. Using comments for TODOs: We can add TODOs for the bash script by using the comments. Simply use # TODO: ... to add TODO for the bash script.

    # TODO: Add error handling to this section
    mkdir /backup
    
  20. Explaining a heredoc:

    # This heredoc prints a multi-line message
    cat <<EOF
    This is a
    multi-line
    message.
    EOF
    

Comments are an essential part of writing readable and maintainable Bash scripts, helping to explain the purpose and functionality of the code.

© 2024 Linux and Python Tutorials

🌱 Powered by Hugo with theme Dream.