Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comments amongst parameters in BASH

Tags:

comments

bash

I want to include command-parameters-inline comments, e.g.:

sed -i.bak -r \
    # comment 1
    -e 'sed_commands' \
    # comment 2
    -e 'sed_commands' \
    # comment 3
    -e 'sed_commands' \
    /path/to/file

The above code doesn't work. Is there a different way for embedding comments in the parameters line?

like image 788
Dor Avatar asked Jun 10 '11 15:06

Dor


2 Answers

If you really want comment arguments, can try this:

ls $(
    echo '-l' #for the long list
    echo '-F' #show file types too
    echo '-t' #sort by time
)

This will be equivalent to:

ls -l -F -t

echo is an shell built-in, so does not execute external commands, so it is fast enough. But, it is crazy anyway.

or

makeargs() { while read line; do echo ${line//#*/}; done }
ls $(makeargs <<EOF
        -l # CDEWDWEls
        -F #Dwfwef
EOF
)
like image 122
jm666 Avatar answered Nov 08 '22 03:11

jm666


I'd recommend using longer text blocks for your sed script, i.e.

sed -i.bak '
    # comment 1
    sed_commands
    # comment 2
    sed_commands
    # comment 3
    sed_commands
' /path/to/file

Unfortunately, embedded comments in sed script blocks are not universally a supported feature. The sun4 version would let you put a comment on the first line, but no where else. AIX sed either doesnt allow any comments, or uses a different char besides # for comments. Your results may vary.

I Hope this helps.

like image 2
shellter Avatar answered Nov 08 '22 01:11

shellter