Okay so I am having this problem. I write up a script to be run in cygwin on Windows. I’ve tried a bunch of basic example scripts in case it was my scripts problem.
So I tried this:
#!/bin/bash
echo -e "Enter your name and press [ENTER]: \c"
read var_name
echo "Your name is: $var_name"
Then I will run it and I enter a name for var_name.
I get this:
$ ./project1.sh
Enter your name and press [ENTER]: Jake
': not a valid identifierad: `var_name
Your name is:
So as far as I understand it I am having a problem with read. I am trying to work on a project for my class, but I can’t seem to figure out why it won’t read it. I followed the book with no triumph then resorted to these examples on the web that do not seem to work for me either. Does anyone have any idea if it is my setup or if I am missing something, thanks.
You should do an
od -xcbon that script. My guesstimate is that it’s almost certainly going to have the wrong line ending character in it.The reason I suggest this is because the line:
looks suspiciously like the two lines:
merged together (where the
.characters indicate something that’s been overwritten).That would be the case if your variable in that line was var_namecarriage-return rather than the more normal
var_name.The fact that it’s Cygwin also points to that conclusion since there’s often trouble when you edit your scripts with a Windows editor which uses CR/LF where Cygwin expects just LF.
I suspect you’ll find that doing an
od -xcbon your actual script shows that you have those Windows line endings on one or more of your script lines.In fact, I just tested this under Ubuntu by putting a
CTRL-Mat the end of just thereadline and the output was (slightly modified to show theCTRL-M):In other words, very similar to what you’re seeing.
As an aside (now that I have access to my Cygwin environment), what you’re seeing is the output:
where the second line overwrites the first, giving:
In other words, the strange word
identifieradis actually made up ofidentifierand the finalad:fromread:. The reason it’s only similar to my example above (as opposed to exact) is because your file name and line number will be different to my little test script.