Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: comment a long pipeline

I've found that it's quite powerful to create long pipelines in bash scripts, but the main drawback that I see is that there doesn't seem to be a way to insert comments.

As an example, is there a good way to add comments to this script?

#find all my VNC sessions
ls -t $HOME/.vnc/*.pid                  \
    | xargs -n1                         \
    | sed 's|\.pid$||; s|^.*\.vnc/||g'  \
    | xargs -P50 --replace vncconfig -display {} -get desktop \
    | grep "($USER)"                    \
    | awk '{print $1}'                  \
    | xargs -n1 xdpyinfo -display       \
    | egrep "^name|dimensions|depths"
like image 272
bukzor Avatar asked Feb 24 '11 05:02

bukzor


People also ask

How do I comment more lines in bash?

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 comment out a paragraph in bash?

In Bash, everything after the hash mark ( # ) and until the end of the line is considered to be a comment. If you have any questions or feedback, feel free to leave a comment.

How can you comment out lines in a shell script?

A single-line comment starts with hashtag symbol with no white spaces (#) and lasts till the end of the line. If the comment exceeds one line then put a hashtag on the next line and continue the comment. The shell script is commented out prefixing # character for single-line comment.

Can you pipe in a bash script?

A pipe in Bash takes the standard output of one process and passes it as standard input into another process. Bash scripts support positional arguments that can be passed in at the command line.


2 Answers

Let the pipe be the last character of each line and use # instead of \, like this:

ls -t $HOME/.vnc/*.pid | #comment here
   xargs -n1 | #another comment 
   ...
like image 103
Eelvex Avatar answered Nov 16 '22 00:11

Eelvex


This works too:

# comment here
ls -t $HOME/.vnc/*.pid |
 #comment here
 xargs -n1 |
 #another comment
 ...

based on https://stackoverflow.com/a/5100821/1019205. it comes down to s/|//;s!\!|!.

like image 25
cheecheeo Avatar answered Nov 16 '22 01:11

cheecheeo