Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put a line comment for a multi-line command [duplicate]

I know how to write a multi-line command in a Bash script, but how can I add a comment for each line in a multiline command?

CommandName InputFiles      \ # This is the comment for the 1st line             --option1 arg1  \ # This is the comment for the 2nd line             --option2 arg2    # This is the comment for the 3nd line 

But unfortunately, the comment after continuation character \ will break the command.

like image 518
Peter Lee Avatar asked Mar 01 '12 19:03

Peter Lee


People also ask

How do you comment multiple lines in Makefile?

' # ' in a line of a makefile starts a comment. It and the rest of the line are ignored, except that a trailing backslash not escaped by another backslash will continue the comment across multiple lines.

How do you write multiple lines in command prompt?

The Windows command prompt (cmd.exe) allows the ^ (Shift + 6) character to be used to indicate line continuation. It can be used both from the normal command prompt (which will actually prompt the user for more input if used) and within a batch file.


1 Answers

This is how I do it. Essentially by using Bash's backtick command substitution one can place these comments anywhere along a long command line even if it is split across lines. I have put the echo command in front of your example so that you can execute the example and see how it works:

echo CommandName InputFiles `#1st comment` \              --option1 arg1 `#2nd comment` \              --option2 arg2 `#3rd comment` 

Another example where you can put multiple comments at different points on one line:

some_cmd --opt1 `#1st comment` --opt2 `#2nd comment` --opt3 `#3rd comment` 
like image 163
Marwan Alsabbagh Avatar answered Sep 30 '22 18:09

Marwan Alsabbagh