Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

case insensitive string comparison in bash

The following line removes the leading text before the variable $PRECEDING

temp2=${content#$PRECEDING}

But now i want the $PRECEDING to be case-insensitive. This works with sed's I flag. But i can't figure out the whole cmd.

like image 892
jjacobs Avatar asked Dec 12 '12 20:12

jjacobs


1 Answers

No need to call out to sed or use shopt. The easiest and quickest way to do this (as long as you have Bash 4):

if [ "${var1,,}" = "${var2,,}" ]; then
  echo "matched"
fi

All you're doing there is converting both strings to lowercase and comparing the results.

like image 61
Riot Avatar answered Sep 30 '22 06:09

Riot