-2

I am creating a script using bash. However during the IF statement it raises this error: [[: not found. I read this topic in other posts but it seems that my predecessors were writing bad their code (e.g. forgetting spaces or else). My question is a little bit different because THE SAME code if run within other parts does not work but if I launch it totally alone it correctly works. Why does this happens? I permit that the variable used is the only one along the whole code.

echo "Digit how many codon positions do you want to use for your partition. [2-3]"
read codonpos
echo $codonpos
[[ "$codonpos" = "2" ]] && echo im here

I also tried:

echo "Digit how many codon positions do you want to use for your partition. [2-3]"
read codonpos
echo $codonpos
if [[ "$codonpos" = "2" ]] 
     then
     echo im here

fi

I repeat you that if launch independently it works but, if this is embedded in a larger code it doesn't.

kvantour
  • 25,269
  • 4
  • 47
  • 72
Claudio21
  • 45
  • 5
  • 1
    Please include a [mre], I can't reproduce this. – oguz ismail Aug 06 '20 at 08:06
  • Here are a couple of questions that would help us out if you could answer them: **(1)** Can you show us the exact error line (i.e. the full line on the screen) **(2)** What is your shebang/ **(3)** Can you show us the lines of code which produce the error, i.e. not a rewrite **(4)** And preferable, can you show us a minimal example that always fails? I.e. Your script, but with all redundant stuff removed, and exact input which you type. – kvantour Aug 06 '20 at 08:14
  • I have a couple of suspicions here: **(1)** you make use of the wrong shebang **(2)** you have a heisenbug, a bug somewhere else in the code and only due to particularities of the input, the codesection with the bug is executed leading to the error **(3)** The unlikely case that you have another blank character instead of a clean space after `[[` (long space, short space, thin space, ...) – kvantour Aug 06 '20 at 08:16
  • @Claudio21 : You obviously did not run it under bash. You can easily test it by doing a `echo $BASH_VERSION` in your script. If the output is empty, it's not bash. – user1934428 Aug 06 '20 at 09:44

1 Answers1

2

I solved the error myself. The issue was due to the presence of an error in the shebang.

I wrote #!/bin/sh instead of #!/bin/bash.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Claudio21
  • 45
  • 5