sh: Don't scan word twice in ${param#%##%%word}.
If word is expanded, use the found end instead of iterating over the data again.