7

good day all

this is a noob question, but I can't seem to find the answer to this anywhere. Other functions like 'cut' or 'awk' or 'sed' are always mentioned but never using the string expression function (I think it is called)

string="hi this is a string.and.it has no purpose other than being here"

searchstring="and"

temp=${string#*$searchstring} # this means everything after "and" will be inserted into temp

echo $temp = "hi this is a string."

but say I want everything before "and" and by using the same method which I used to get the temp variable result, thus I want

echo $temp = ".it has no purpose other than being here"

I have tried

temp=${string%*$searchstring} (as I understand, # is from front and % is back)

but this returns only string's contents unaltered

Also, man pages, what is this called, this string expression function, where can I find more indepth information?

11

You have to place the asterisk to the other end of the string. The asterisk stays for "whatever", so

${string%$searchstring*}

means "remove from string everyting from the searchstring onwards".

This is documented in man bash under "Parameter Expansion".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.