How to input a comment on csh?
Asked Answered
K

4

8

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:~ # 
Kaikaia answered 14/10, 2013 at 16:37 Comment(6)
just for fun in interactive csh you can delete all files ending with a hash with rm *#. Put that in a script and something different happens.Enneahedron
@george If it's true, it's really awful! And I don't want to prove it myself!Kaikaia
oh yes, something I learned the hard way many years ago creating a script to clean up emacs autosave files.. bash seems to need a space in front of the # to indicate a comment by the way.Enneahedron
@george: A # comment can be at the beginning of a line. Otherwise, you need whitespace separating it from the preceding token. echo foo#bar prints "foo#bar"; echo foo #bar prints "foo".Forethoughtful
Please read my updated answer. I was mistaken about how : works in csh (as opposed to tcsh).Forethoughtful
In bash and zsh, the behavior is configurable; see the option variously spelled interactive_comments, interactive-comments, or INTERACTIVE_COMMENTS.Forethoughtful
F
12

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

Forethoughtful answered 14/10, 2013 at 18:16 Comment(2)
I think now I understand why people stop using csh. And just for note, I wanted this for copy & paste a lot of code just as a quick REPL.Kaikaia
@SethMMorton: Not sure why this and an unrelated answer of mine were downvoted 12 seconds apart. Coincidence?Forethoughtful
D
2

To use # for interactive comments in tcsh or csh, this seems to work in most cases:

alias '#' 'echo \!* >> /dev/null'

It can be run by the user interactively or placed in a .tcshrc or .cshrc configuration file as appropriate.

(The alias can obviously be named differently as you might desire; normal restrictions apply.)


Note: as Keith Thompson noted above, this will still give errors if your comment includes redirect characters such as > or >>, but used this way does not appear to actually create an undesired redirect file. Surrounding the comment in single quotes (') is still a workaround.

Darken answered 3/12, 2017 at 19:19 Comment(1)
Thanks! You rock. The best answer here, I hope it will get noticed.Flex
K
1

For the reference, I like to note my current workaround. It's using echo.

#!/bin/tcsh
echo "This is comment line."
echo "But still, beware... Because `expression` is still being evaluated.

This is the best way I could find.

Kaikaia answered 18/10, 2013 at 18:26 Comment(1)
If it's in a script, as implied by the #!/bin/tcsh line, you can just use normal # comments.Forethoughtful
D
-2

Check if your script has UNIX EOL Format. Has some problem with Windows EOL Format.

Disseminule answered 27/4, 2016 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.