': not a valid identifier for read
I use Notepad++ and type:
read s echo "$s"The Output is:
And then I try to put semicolon:
read s;
echo "$s"and the Output is:
I save the file as q.sh and run with cygwin but still I cannot use read operator so I'm getting confused. I have tried other editors like vi also but the output is the same.
What is wrong? How to make it work?
32 Answers
The text file that constitutes the shell script was written in Notepad++. This editor saves files as DOS text files by default which, from the Unix point of view, has a superfluous carriage return (\r) at the end of each line. This confuses sh.
To remove it:
$ tr -d '\r' <q.sh >q-new.sh
$ mv q-new.sh q.shThe script does lack a #!-line (as pointed out in comments), but as long as you run it with an explicit interpreter (sh q.sh), this is not needed.
Adding the line
#!/bin/shwould (after chmod +x q.sh) make running it as ./q.sh behave exactly the same as running it with sh q.sh.
Another way to convert the carriage return for Unix, if you have access to Notepad++. In Notepad, go on : Edit -> EOL conversion -> Unix (LF)
In French, it's : Edition -> Convertir les sauts de ligne -> Converti au format Unix (LF)
It can be faster.