Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash command spanning multiple lines with several lines of comments in-between [duplicate]

Tags:

bash

Possible Duplicate:
Bash: How to Put Line Comment for a Multi-line Command

I would like to do something like this

sudo apt-get install \   #a very long description   #of the package   #that spans multiple lines   pkg1 \ #maybe I want an inline comment also   #another description that   #spans multiple lines   pkg2 

Note that I'm not just interested in the apt-get command.

like image 838
Xu Wang Avatar asked Oct 10 '12 07:10

Xu Wang


People also ask

How do I comment multiple lines in a bash script?

In Shell or Bash shell, we can comment on multiple lines using << and name of comment. we start a comment block with << and name anything to the block and wherever we want to stop the comment, we will simply type the name of the comment.

How do you split a long command sequence into multiple lines?

To split long commands into readable commands that span multiple lines, we need to use the backslash character (\). The backslash character instructs bash to read the commands that follow line by line until it encounters an EOL.

Can a bash variable have multiple lines?

Although Bash has various escape characters, we only need to concern ourselves with \n (new line character). For example, if we have a multiline string in a script, we can use the \n character to create a new line where necessary.

How do you write a multiline command in shell?

Using a Backslash. The backslash (\) is an escape character that instructs the shell not to interpret the next character. If the next character is a newline, the shell will read the statement as not having reached its end. This allows a statement to span multiple lines.


1 Answers

As far as I know Bash ignores everything after the '#' in a single command, and multilining won't change that. However you can probably achieve the same level of expression using bash arrays:

packagelist=(   package1 # Inline Comments   # Multiline Comments too   package2   # Package description goes here   # Detailed descriptions.. ) sudo apt-get install ${packagelist[@]} 
like image 176
phininity Avatar answered Sep 21 '22 23:09

phininity