': not a valid identifier for read

1

1

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?

Jan Nutcha

Posted 2017-08-26T20:15:57.167

Reputation: 13

3

Your file has windows-style line-endings. You need to remove them. See instructions here.

– John1024 – 2017-08-26T20:43:56.307

You are also missing #!/bin/bash as the first line of the shell script. – DavidPostill – 2017-08-26T21:13:28.990

Oh Ok I got it thank you John1024 and DavidPastill – Jan Nutcha – 2017-08-27T07:52:55.443

Answers

1

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.

Kusalananda

Posted 2017-08-26T20:15:57.167

Reputation: 1 941

1

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.

Kevin Brun

Posted 2017-08-26T20:15:57.167

Reputation: 11