Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Some commands, such as rsync, take in lists as an argument. Can these files contain unix comments like #?

share|improve this question

Unless the documentation for the command explicitly mentions that it can contain comments, assume that it cannot.

share|improve this answer
Concur. Generally, the respective manpage of the command will detail whether you can add comments to a read file, and if so how it identifies a line as a comment. – Shadur Oct 13 '11 at 9:58

What I usually do in cases like this, is build an arguments array (which can take comments) and then pass those along to rsync, for instance.

    # Show me what you're doing
    # All HTML and resources
    *.html *.css *.js
    # All PHP source code
    # To the live server
rsync "${rsync_args[@]}";
share|improve this answer

If I understand you correctly, you want to add comments about files in the file list. This is not possible, because when shell encounters the # character, it will ignore all remaining character on the command line. One way to achieve this is to use rsync with --files-from=filelist argument. The filelist file can contain comments.

On the other hand, if you want to embed # characters in the filename, then this will work. For example, with bash:

touch a#b 
touch 'a #b' 
touch "a #b"
share|improve this answer
See e.g. the --include-from argument of rsync. – Ignacio Vazquez-Abrams Oct 13 '11 at 12:12

Ignacio Vazquez-Abrams answer is the safest, but if you really want to do it (as I do in some instances), a simple workaround can easily get at least part-way there by dealing with # comments which take up the entire line.

Things can get a bit gnarly if you want to handle comments which follow the real list data-lines, unless you are certain that your data-lines contain no embedded # chars.

You can use process substitution and a simple helper script.

Here are some examples. The grc before diff generates coloured output.
I've used bash functions, but you can, of course, save them to a script file.

# nocom handles comment-only lines (ie. no dat)
#       it ignores any # chars embedded in your data lines.
  nocom() { sed -e '/^[[:space:]]*#/d' "$1"; }

# NOCOM handles handles comments which occurr after your data   
#       as well as comment-only lines.  
#       Do not use NOCM unless you are CERTAIN that your data
#       lines contain no  # chars.
  NOCOM() { sed -n '/^[[:space:]]*#/d; s/^\([^#][^#]*[^[:space:]]\)[[:space:]]*#.*/\1/; p' "$1"; }

printf '# a bit of hokus-pokus
        # you see the coments here, 
        # but the calling program will not.
$HOME/bin/kadabra  # comment after data
$HOME/bin/sim# another comment after data
' >file

echo == nocom ==
cat <(nocom "file")
echo == NOCOM ==
cat <(NOCOM "file")
echo == diff ==
grc diff "file" <(NOCOM "file")

Here is the output:

== nocom ==
$HOME/bin/kadabra  # comment after data
$HOME/bin/sim# another comment after data
== NOCOM ==
== diff ==
< # a bit of hokus-pokus
<         # you see the coments here, 
<         # but the calling program won't.
< $HOME/bin/kadabra  # comment after data
< $HOME/bin/sim# another comment after data
> $HOME/bin/kadabra
> $HOME/bin/sim
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.