bash - regex trim untill symbol

Viewed 31

trying to trim untill and including symbol " - " .
for some reason, when i add ^(.*?) bash will not modify the varieble.
GNU bash, version 5.1.16(0)-release (amd64-portbld-freebsd12.2)

fname="text1 - text2.jpg"
outp="${fname//^(.*?)\s-\s/}"
echo $outp

expected result: text2.jpg
any suggestions?

1 Answers

You appear to be trying to use a regexp (specifically a PCRE or a BRE or ERE with undefined behavior) instead of a globbing pattern in your command. See https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html.

Is this what you're trying to do?

$ echo "${fname/* - }"
text2.jpg

$ echo "${fname#* - }"
text2.jpg

$ echo "${fname/*[[:space:]]-[[:space:]]}"
text2.jpg
Related