Inline bash in command comments PDF Print E-mail

I spent many time in search how add comment into long bash commands. For one my script, where it was very important for me even special flag was added which just ignored inner (look -c switch in HuRegRep).

But I look something similar to (warning, example does not work):

echo string0 \
	string1 /* Second parameter required because... */ \
	string2

In fact all is very simple - bash and sh just do not support such comments at all! At least I make such decision by many ununswered questions in forums and maillists.

In speaking and brainstorming with friends born very interesting idea, which further became very elegant solution on my mind:

unset comment
echo string0 \
	string1 ${comment:"Second parameter required because..."} \
	string2

Few additional words:

  1. Firstly, as you already understood, this is not true comment! Instead we just use variable substitution which will be propogated in empty string! And this look well to read in code.
  2. Because it is simple variable it may have arbitrary name (in standard names restrictions off course), not exact comment. And it absolutely must be (I think well short candidates there c or _c). Meantime comment reflect idea appearance it there.
  3. And last. "unset comment" before main comment is just assurance what variable appeared empty and can't mangle command itself. If script is small and simple or you sure what it variable not used befor you may safely ommit it line.
Share/Save/Bookmark
Written by Павел Алексеев aka Pahan-Hubbitus   
Saturday, 08 January 2011 19:21
Last Updated on Sunday, 09 January 2011 01:17
 

Add comment


Security code
Refresh