I'm trying to use Bash replacement to replace the whole string in variable if it matches a pattern. For example:
pattern="ABCD"
${var/pattern/}
removes (replaces with nothing) the first occurrence of $pattern in $var
${var#pattern}
removes $pattern in the beginning of $var
But how can I remove regex pattern "^ABCD$" from $var? I could:
if [ $var == "ABCD" ] ; then
echo "This is a match." # or whatever replacement
fi
but that's quite what I'm looking for.
"${var/$pattern/}"? Can you clarify with some example inputs.ifstatement.[ "$var" = ABCD ] && var=""is slightly more terse but the same idea.$var-- they expand to a string that comes from$varand has the corresponding change, but$varitself remains unchanged -- later instances of$varwill not be affected.