How to remove prefix from string in Bash?
For example:
STRING1="My name is James" echo "${STRING1}" # My name is James NAME="${STRING1#???}" echo "${NAME}" # James
Alternative: FOO=${FOO//$WORDTOREMOVE/}
FOO=${FOO//$WORDTOREMOVE/}
$ STRING1="My name is James" $ NAME="${STRING1//My name is /}" $ echo "${NAME}" James
Update as per @gniourf_gniourf's suggestion : FOO=${FOO/#$WORDTOREMOVE/}
FOO=${FOO/#$WORDTOREMOVE/}
$ NAME="${STRING1/#My name is /}" $ echo "${NAME}" James
To do so, you have to escape special symbols in the prefix string.
NAME="${STRING1#My\ name\ is\ }" echo "${NAME}" # James
The same thing with suffices.
Alternative:
FOO=${FOO//$WORDTOREMOVE/}
Update as per @gniourf_gniourf's suggestion :
FOO=${FOO/#$WORDTOREMOVE/}