1

I'm trying to compare a substring of one string variable to a whole variable, and it just always comes out as false.

COMP='<'
if [[ '${SNIP:0:1}' = '$COMP' ]] ;then
  LENG=6
elif [[ '${SNIP:1:1}' = '$COMP' ]] ;then
  LENG=7
else
  LENG=8
fi
echo $SNIP
echo ${SNIP:0:1}
echo ${SNIP:1:1}
echo $COMP
echo $LENG

I've tried as well with just comparing the substrings with '<', but this instead always returns true.

I would expect the output to be

3<a
<
<
7

but the output is

3<a
<
<
8

and I don't know what is messing up. Please help. Thank you.

xehcimal
  • 11
  • 2

1 Answers1

0

Just use double quotes:

COMP='<'
if [[ "${SNIP:0:1}" = "$COMP" ]] ;then
  LENG=6
elif [[ "${SNIP:1:1}" = "$COMP" ]] ;then
  LENG=7
else
  LENG=8
fi
echo $SNIP
echo ${SNIP:0:1}
echo ${SNIP:1:1}
echo $COMP
echo $LENG

Single quotes won't interpolate anything, but double quotes will.

https://stackoverflow.com/a/6697781/344480

Matthias
  • 7,432
  • 6
  • 55
  • 88