ECHO() {
  if [ "$VERBOSE" == 'yes' ] ; then
    echo "$*"
  fi
}

goodsed() { # just to add one backslash before minus
# BASH variant of: 'echo "$1" | sed -e 's/-/\\\\-/g'`
  STRING="$1"
  QSTRING=${#STRING}
  echo "	string=$STRING QSTRING=$QSTRING"
  RESULT=''
  i=0
  k=0
  while [ $i -lt $QSTRING ] ; do
    ECHO DEBUG i-symbol="${STRING:i:1}"
    if [ "${STRING:i:1}" == '-' ] ; then
      RESULT="$RESULT"'\'
      let "k++"
    fi
    RESULT="$RESULT${STRING:i:1}"
    let "i++"
    let "k++"
  done
  echo -n "	result=$RESULT size=${#RESULT}"
  echo " ${RESULT:0:$k}"
}

badsed() { # just array indexes are used for RESULT
  STRING="$1"
  QSTRING=${#STRING}
  echo "	string=$STRING QSTRING=$QSTRING"
  RESULT=''
  i=0
  k=0
  while [ $i -lt $QSTRING ] ; do
    ECHO DEBUG i-symbol="${STRING:i:1}"
    if [ "${STRING:i:1}" == '-' ] ; then
      RESULT[k]='\' #! not working :(
      let "k++"
    fi
    RESULT[k]="${STRING:i:1}" #! not working :(
    let "i++"
    let "k++"
  done
  echo -n "	result=$RESULT size=${#RESULT}"
  echo " ${RESULT:0:$k}"
}

goodrev() { # BASH variants of `echo "$1" | rev`
  STRING="$1"
  QSTRING=${#STRING}
  echo "	string=$STRING QSTRING=$QSTRING"
  RESULT=''
  i=0
  while [ $i -lt $QSTRING ] ; do
    ECHO DEBUG i-symbol="${STRING:QSTRING-i-1:1}"
    RESULT="$RESULT${STRING:QSTRING-i-1:1}"
    let "i++"
  done
  echo "	result=$RESULT size=${#RESULT}"
}

badrev() {
  STRING="$1"
  QSTRING=${#STRING}
  echo "	string=$STRING QSTRING=$QSTRING"
  RESULT=''
  i=0 
  while [ $i -lt $QSTRING ] ; do
    ECHO DEBUG i-symbol="${STRING:QSTRING-i-1:1}"
    RESULT[i]="${STRING:QSTRING-i-1:1}"
    let "i++"
if [ $i -ge $QSTRING ] ; then
echo "XXX-break i=$i result=$RESULT size=${#RESULT}"
break # XXX
fi
  done
  echo "	result=$RESULT size=${#RESULT}"
}

if [ $# -gt 0 ] ; then
  VERBOSE="$1"
  echo
  echo "VERBOSE=$VERBOSE"
fi

echo GOOD variants of sed
goodsed "Bb12345678"
echo
goodsed "bB123-4567-8"

echo BAD variants of sed
badsed "Bb12345678"
echo
badsed "bB123-4567-8"

echo GOOD variant of rev
goodrev "123456789"

echo BAD variant of rev
badrev "123456789"
