Problems with simple script in cygwin

Hello!
I have somo problems with simple scripts like this:

#!/bin/bash
echo -n "Enter your name and press [ENTER]: "
read var_name
echo "Your name is: $var_name"

When I try to run it, this error occurs: ':not a valid identifier var_name.
Why?? (I work in cygiwin)
Is there anybody out there that can help me??
Thanksss
:confused:

Try this

read -p "Enter your name and press                                           : " var_name
echo "Your name is: $var_name"

Thank you for your reply but it displays the same error!!!!

I also use cygwin. When I copyed your code, it worked.

Does it work?!?!??!
Is it possible that "my" cygwin doesn't recognize the read command??

What did you type to execute the script?

Did you use a unix editor to create the script or has it come from MSDOS?

I write the code on block notes and then in the shell bash I write : sh name_file.txt

Works fine on my PC with cygwin :

$ which bash
/usr/bin/bash
$ cat blianna.sh
#!/usr/bin/bash
echo -n "Enter your name and press [ENTER]: "
read var_name
echo "Your name is: $var_name"

$ blianna.sh
Enter your name and press [ENTER]: Jp
Your name is: Jp
$
  • Verify the path for bash
  • Run your script directly like me.

Jean-Pierre.

Use a unix editor or try to convert the file with:

tr -d '\r' < dosfile > unixfile

I agree with franklin52 without knowing about the editor called "block notes".
The script file could well be in MSDOS format where each line is terminated with carriage-return/linefeed when unix expects each line to be terminated with just linefeed. This will upsed the shell interpreter.

franklin52 post is suggesting conversion of the script from MSDOS to unix format. This is a good suggestion.

If this doesn't work, try creating a script file under a new name with your favourite unix editor (e.g. "vi") and cut/paste your post into that file.
Then make the script executable:
chmod 755 scriptname
Then execute the script from the current directory:
./scriptname

Good luck.

If you like to use your script/rule with some program then use it. If you like to run it using bash, then use it, not sh:

bash scriptname

or make your script executable as methyl has written and run it. In this case caller shell read the first line, find your programpath (/usr/bin/bash), start it with this scriptfile, just like previous commandline example has written.

chmod a+rx scriptname
./scriptname