11.6 C
Jaipur
Wednesday, December 8, 2021

Shell Script Put Multiple Line Comments under Bash/KSH

Must read

I would like to use multiline comments under shell script. Under C / C++ I can use the following format:
/*
my c code
comment # 2
blah
foo bar
….
*/

How do I put multi line comment under a shell script? Can you tell method to set multiline shell script comments? How do you comment out multiple lines in shell script?

In bash shell, we can comment on multiple lines using various methods. Let us see how put comment out multiple lines in shell script running under Linux or Unix-like systems.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements None
Est. reading time 2 mintues

Multiline shell script comments vs. single line comment

By default shell script can be commented out prefixing # character, for example:
# my comment goes here

Bash Shell Script Put Multiple Line Comment Syntax

For multi line comment use the following syntax:

#!/usr/bin/env bash
# my comment  1
# my comment  2
# my comment  N

Multi-Line Comment in Shell Script Using the HERE DOCUMENT

However, you can use HERE DOCUMENT feature as follows:

#!/bin/bash
echo "Say Something"
<<COMMENT1
    your comment 1
    comment 2
    blah
COMMENT1
echo "Do something else"

This type of redirection tells the shell to read input from the current source (HERE) until a line contain only word (HERE) is seen. HERE word is not subjected to variable name, parameter expansion, arithmetic expansion, pathname expansion, or command substitution. All of the lines read up to that point are then used as the standard input for a command. Files are processed in this manner are commonly called here documents. If you do not want variable name, parameter expansion, arithmetic expansion, pathname expansion, or command substitution quote HERE (COMMENTS) in a single quote:

<<'COMMENTS'
text1
text2
testN
$varName
COMMENTS

Bash comment block example

It is important that you put EOF in a single quote (EOF) to avoid command execution and phrasing. Here is block comments in a shell script example:

#!/usr/bin/env bash
echo "*** Before comment block ***"
: <<'EOF'
CODE block starts
CODE block ends here
EOF
echo "*** After comments block ***"

Writing multi-line comments in Bash scripts

Multiline shell script comments

Another option as pointed out by Ikram in the comments section below:

#!/bin/bash
foo=bar
: '
This is a test comment
Author foo bar
Released under GNU 
'
 
echo "Init..."
# rest of script

Please note that the : is shell builtin command. From the bash(1) man page:

: [arguments]
No effect; the command does nothing beyond expanding arguments
and performing any specified redirections. A zero exit code is
returned.

So the syntax is:

Null command used in your shell script to put multiple comments. The ‘:‘ has no effect. In other words, the command does nothing and it always exit with succeed code.

Examples

#!/bin/bash
#
# example about calling this stackscript
# Multi-line comment
<<COMMENT1
_BOX="linode-nixcraft-03"
_BOX_VLAN="172.0.0.100/24"
linode-cli linodes create 
--type g6-standard-2 
--image linode/ubuntu20.04 
--region us-southeast 
--root_pass 'ROOT_PASS_HERE' 
--authorized_keys 'ssh-ed25519 SSH_PUB_KEY_HERE [email protected]' 
--booted true 
--backups_enabled true 
--private_ip true 
--interfaces.label "" 
--interfaces.purpose public 
--interfaces.ipam_address "" 
--interfaces.label vlan003 
--interfaces.ipam_address "${_BOX_VLAN}" 
--interfaces.purpose vlan 
--stackscript_id 874407 
--stackscript_data "{"_BOX_NAME": "${_BOX}"}" 
--label "${_BOX}"
COMMENT1
 
# Main script starts below
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>/root/StackScript.log 2>&1
 
## SET ME for each server using the cli but some values are static ##
_hostname="" # get from the cli
_admin_pub_in="eth0"
_admin_vlan_in="eth1"
_admin_vlan_in_cidr="172.0.0.0/24"
_linode_nb_cidr="192.168.255.0/24"
_my_admin_pub_ip="$(hostname -I | awk '{ print $1}')"
_my_admin_pvt_ip="$(hostname -I | awk '{ print $2}')"
_my_admin_vlan_ip="$(hostname -I | awk '{ print $3}')"

Patreon supporters only guides 🤓

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare
  • Join my Patreon to support independent content creators and start reading latest guides:

Join Patreon

# rest of the script below

Conclusion

It would be best if you documented your script and writing comments considered as a best practice. Other sysadmins and developers might use comments to understand your scripts. As explained eariler, anything is written after # and till the end of the line is nothing but comments.

You learned about multi line shell script comments possibilities. However, shells do not offer a multiline comment syntax. Hence, we learned and used various techniques such as here-documents to make multiline “comments” under Linux or Unix-like systems.


ADVERTISEMENT

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article