2

I would like to execute a quite simple test by using :

YY=${array[17]}
echo $YY

if [ "$YY" -ne 0 ]
then
    echo "do.."
else
    echo "here"
fi

But this code returns :

0
./script.sh: line 303: $'[\302\2400': command not found

What I do wrong ?

6
  • 1
    Dunno what that error is but if YY isn't an integer then using single [ will fail. In almost all cases it is better the use double [[ i.e if [[ "$YY" -ne 0 ]] Commented Mar 23, 2015 at 9:59
  • Thank for your reply but the problem stay the same, it returns line 302: $'[[\302\2400': command not found Commented Mar 23, 2015 at 10:23
  • Could you give an example of YY value - since you are calling an array index that we cannot see. Commented Mar 23, 2015 at 10:43
  • What editor do you use to edit your scripts? Commented Mar 23, 2015 at 10:49
  • 2
    $'\302\240' is the character U+00A0 NO-BREAK SPACE. Replace the unbreakable space by an ordinary space. Commented Mar 23, 2015 at 11:18

2 Answers 2

4

$'[\302\2400' is bash's way of writing a three-character string: a "[" followed by a non-breaking space (unicode U+00A0; in HEX UTF-8 that's c2 a0, but the shell prints it in octal as 302 240), and finally the digit "0". Based on this, I'm pretty sure you have a non-breaking space between [ and "$YY", and you just need to replace that with a normal space.

Sign up to request clarification or add additional context in comments.

Comments

1

Sounds like you have some weird chars in your number string...Maybe try using REG_EX to extract numbers from a string..

YY=${array[17]}
echo "\"$YY\""

#Use reg ex to extract number and leave behind garbage
REG_EX="([0-9]+)" 

if [[ $YY =~ $REG_EX ]]
then
  YY=${BASH_REMATCH[1]}
  if [ "$YY" -ne 0 ]
  then
    echo "do.."
  else
    echo "here"
  fi
else
  echo "Trouble extracting number from YY"
fi

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.