質問

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:~ # 
役に立ちましたか?

解決

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, such as bash.) 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

他のヒント

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.

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.

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top