0

I have a file, named myscript.sh. It contains if statement in bash, but it does give an error.

Script text:

#!/bin/bash
echo -n "Enter a number: "
read VAR
if [[ $VAR -gt 10 ]]
then
  echo "The variable is greater than 10."
fi

Error text:

./myscript.sh: line 4: syntax error in conditional expression
'/myscript.sh: line 4: syntax error near `]]
'/myscript.sh: line 4: `if [[ $VAR -gt 10 ]]

What should I do in this case?

BBB
  • 13
  • 3
  • how are you invoking the shell script? the current code works for me (ie, does not generate any errors); also, the provided code does not include an 'else' block so the description - It contains `if..else..fi` - doesn't make sense – markp-fuso May 05 '22 at 13:45
  • I invoke my script using ```./myscript.sh``` – BBB May 05 '22 at 13:52
  • It works! Which bash version are you using? – Antonio Petricca May 05 '22 at 14:06
  • 1
    I suspect hidden characters -- something like a non-breaking space where a normal ASCII space should be. If you use `cat -A` to look at the file, does it come out as it should? – Charles Duffy May 05 '22 at 14:07
  • See your code working correctly running unmodified at https://ideone.com/bnfLXW. We need a [mre] that lets us reproduce the same problem ourselves for a question to be answerable. – Charles Duffy May 05 '22 at 14:07
  • 1
    I can generate the same exact errors if I first run `unix2dos myscript.sh` (ie, add windows/dos line endings `\r\n` to the file); running `od -c myscript.sh` should show `\r` characters in the output; easiest way to fix the issue: `dos2unix myscript.sh`; let us know if you don't have `dos2unix` on your system – markp-fuso May 05 '22 at 14:11
  • Heh, right! I missed the leading `'`s, but that does indeed make it a CRLF problem. – Charles Duffy May 05 '22 at 14:11
  • 1
    @BlendaB, ...btw, if you're using `cat -A` as I suggested earlier, the `\r`s that @markp-fuso is talking about will show up in the output as `^M`s. – Charles Duffy May 05 '22 at 14:13

0 Answers0