| |

Add Inline Comments for Multi-line Command in Bash Script

In Bash script, it is common that multiple small commands run together connected by pipes (|) and the whole command is quite long. For clarity, we may write the command in multiple lines. How to add comments for these long multi-line commands? In Bash, the content after # in a line is the comment. Using a single comment line before the multi-line command is not convenient as the command may be quite complex. The best way is still inline comments as we do in many other programming languages. In this post, we discuss 2 techniques used in 2 different scenarios.

Inline comments for multi-line pipe-connected commands

For pipe-connected commands, we use the interesting feature of bash that the pipe | connects 2 subshells and the subshells can have empty lines before it. So we can add a comment after the | and a new line then the next command. One example is as follows.

echo -e "Aabcbb\nAabcbD\nAabcbb" |  # generate the content
     tr a-z A-Z |                   # translate to upper case
     sort |                         # sort the text
     uniq                           # remove duplicated lines

Grammatically, the commend at the end of each line after | is actually part of the next command. But it is natural for readers of the script to consider the comment to describe the command before the |. Cool, right?

Inline comments for multi-line commands separated by \<newline>

Now, we don’t have the nice grammar help from the |. We need the techniques making use of the command substitution feature. The command of the subshell can include comment too and the comment ends at the end of the subshell command. So, a command as follows is valid and the comment in the substitution command can be considered as the “inline comment” for that line.

perl -0777 `# whole input as a block` \
 -p `# loop and print line also` \
 -e 's|<PRE>[\s{<BR>}{<HR>}]*</PRE>||g' 

But note that this technique is expensive because it creates a subshell for each of such “inline comments” during execution. It is only suitable if the commands performance or cost is not a problem.

Similar Posts

  • |

    Git Merging FAQs

    git merge is frequently used during development of projects managed by git. There are many common questions about git merge and I find most are solved and discussed on StackOverflow. Here, I summarize useful ones so that others who want to learn git merge need to do the search again. Merge, rebase, fast forward and…

  • Managing LVM

    Any tutorials or tips on how to manage LVM? LVM provides a flexible and easy way to management storage disks on Linux. It also provides a set of tools for management. Here are some tutorials and references for managing LVM: LVM man pages: https://www.systutorials.com/docs/linux/man/8-lvm/#lbAL Manage Disk Volume in Linux: http://www.hongkedavid.com/blog/disk_volume.html LVM with Xen: https://www.systutorials.com/b/linux/tag/lvm/ Remove missing phyiscal volumes…

  • Auto indenting for OCaml code in Vim

    How to set up auto indenting for OCaml code in Vim? The built-in indenting in Vim for OCaml seems not very good. Please check the post at https://www.systutorials.com/5212/auto-indenting-for-ocaml-code-in-vim-with-ocp-indent/ Read more: Auto Indenting for OCaml Code in Vim with ocp-indent Vim Indenting C/C++ Code How to get the assembly code for OCaml code generated by ocamlopt?…

  • Designing posters – SysTutorials QA

    Some tutorials or materials for designing posters? Designing conference posters by Colin Purrington: http://colinpurrington.com/tips/academic/posterdesign Scribus – an open source desktop publishing tool: http://www.scribus.net/canvas/Scribus Inkscape is a good tool for editing vector figures. GIMP is a great tool for editing images. Read more: Squeezing Space in LaTeX – SysTutorials QA Free server images – SysTutorials QA…

Leave a Reply

Your email address will not be published. Required fields are marked *