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:
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 :)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With