commentscsh

How to input a comment on csh?


In bash, I used # to input comment. Even on interactive session.

bash-3.2$ #
bash-3.2$ #
bash-3.2$ #
bash-3.2$ 

csh spits error for this. How can I input some comment on interactive csh session? In other words, I am looking for a way to make 100% sure comment in csh.

root@freebsd9:~ # #
#: Command not found.
root@freebsd9:~ # # 3e
#: Command not found.
root@freebsd9:~ # #
#: Command not found.
root@freebsd9:~ # 

Solution

  • Interactive csh or tcsh doesn't do comments. The # character introduces a comment only in a script. (This is unlike the behavior of sh and its derivatives. In bash and zsh, the behavior is configurable.) Quoting the csh man page (from Solaris 9, one of the remaining systems where csh is not just a symlink to tcsh):

    When the shell's input is not a terminal, the character # introduces a comment that continues to the end of the input line. Its special meaning is suppressed when preceded by a \ or enclosed in matching quotes.

    The point, I think, is that interactive commands don't need comments.

    If you're using tcsh, you can do something similar with the built-in : command, which does nothing:

    % : 'This is not a comment, but it acts like one.'
    

    (where % represents the shell prompt and : is the command). Quoting the argument is a good idea; otherwise, though the command is not executed, it can have some effect:

    % : This will create the file "oops.txt" > oops.txt
    

    Note that since : is a command, it must be followed by a space.

    The : command was originally introduced in a very early version of the Bourne shell, or perhaps even before that.

    However, the /bin/csh version of the : command does not permit any arguments, making it useless as a comment replacement:

    csh% : 'This will not work.'
    :: Too many arguments
    csh% 
    

    (I didn't realize that when I initially posted this answer. I must have tested it with tcsh rather than a true csh.)

    Since : doesn't work in pure csh, the next best solution is probably to use echo and redirect the output:

    csh% echo 'This is not a comment, but it acts like one.' > /dev/null
    

    Obligatory link: http://www.perl.com/doc/FMTEYEWTK/versus/csh.whynot