You may match the substring with a regex you have and access Group 1 using ${BASH_REMATCH[1]}
:
k="abc=value1"
rx='abc=(.*)'
if [[ $k =~ $rx ]]; then
echo ${BASH_REMATCH[1]};
fi
# => value1
See the online demo
Note that it is the most straight-forward way to use a regex by declaring it in a separate variable, so that the code works as expected in all Bash versions supporting the =~
match operator. Beginning with Bash 3.2, [[ $k =~ "abc=(.*)" ]]
like code will trigger literal string comparison rather than a regex comparison:
f. Quoting the string argument to the [[
command's =~
operator now forces
string matching, as with the other pattern-matching operators.
A regex makes sense if you have a complex scenario. In your case, string manipulations like the one pynexj suggested will also work:
k="abc=value1"
echo ${k#*=}
# => value1
See another demo online. Here, #
triggers substring removal of the shortest substring between the start of string and the first =
char (including it). See documentation:
Substring Removal
${string#substring}
Deletes shortest match of $substring from front of $string.