I have the following script which works:
x=10
echo $x
now=$(date +'%Y-%m-%d')
echo $now
However, when I add a comment line at the beginning:
# comment
x=10
echo $x
now=$(date +'%Y-%m-%d')
echo $now
I get the following:
x=10: command not found
x: undefined variable
Why is the addition of the comment causing the script to fail?
if I do the following it works:
x=10
echo $x
now=$(date +'%Y-%m-%d')
# comment here
echo $now
This is a quirk of csh. (Stop using csh!) csh will process a script that does not begin with a ‘#’ using a “standard shell” (quoting from the csh manpage.) When the script begins with ‘#’, csh parses it. Your script is not valid csh.
You should probably add a shebang line to avoid this type of issue. That is, make the first line: