Celeb Glow
news | March 12, 2026

': not a valid identifier for read

I use Notepad++ and type:

 read s echo "$s"

The Output is:

<code>': not a valid identifier</code>

And then I try to put semicolon:

read s;
echo "$s"

and the Output is:

enter image description here

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?

3

2 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.sh

The 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/sh

would (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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy