Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"bad variable name" using "read var"

I am getting confused about Linux shells. It may be that I oversee something obvious as a Linux noob.

All I want is the following script to run:

#!/bin/bash
echo "Type some Text:"
read var
echo "You entered: "$var

Here is the situation:

  • Installed Ubuntu Server 14.04 in a VirtualBox on windows
  • Installed it with this packages
  • A SAMBA mounted on /media/buff
  • The script is on /media/buff/ShellScript/test.sh
  • made executable by "sudo chmod a+x /media/buff/ShellScript/test.sh"
  • The rest is default
  • I am using PSPad on windows to edit the script file

I read about the dash but I'm not getting it. Here are some variations:

Using sh to launch

user@Ubuntu:/media/buff/ShellScript$ sh test.sh
Type some Text:
:bad variable nameread var
You entered:

Using bash to launch:

user@Ubuntu:/media/buff/ShellScript$ bash test.sh
Type some Text:
': Ist kein gültiger Bezeichner.var (means no valid identifyier)
You entered:

Changed the Shebang in the script to "#!/bin/sh", Using sh to launch

user@Ubuntu:/media/buff/ShellScript$ sh test.sh
Type some Text:
:bad variable nameread var
You entered:

I searched across the Internet for hours now and I assume, that the script itself is ok, but there are missing some environment settings. I used "sudo dpkg-reconfigure dash" to set dash as default shell (which I think is ubuntu default anyway)

sadface panda :)

like image 829
VapoRizer Avatar asked Oct 24 '15 16:10

VapoRizer


1 Answers

There are most probably carriage returns (CR, '\r') at the end of your lines in the shell script, so the variable is trying to be read into var\r instead of var. This is why your error message is meaningless. A similar error should look like this:

run.sh: 3: export: [... the variable name]: bad variable name

In your case bash throws an error because var\r is illegal for a variable name due to the carriage return, so it prints

test.sh: 3: read: var\r: bad variable name

but the \r jumps to the beginning of the line, overwriting the start of the error message.

Remove the carriage returns fom the ends of lines, possibly by using the dos2unix utility.

like image 75