12

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?

asked Nov 26, 2015 at 13:09

1 Answer 1

24

You have to place the asterisk at 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".

Tms91
2084 silver badges13 bronze badges
answered Nov 26, 2015 at 13:18

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.