bash - 字符串操作在子字符串之前或之后获取字符串

Cyb*_*beX 8 linux bash

大家好

这是一个菜鸟问题,但我似乎无法在任何地方找到答案。其他函数如 'cut' 或 'awk' 或 'sed' 总是被提及但从未使用字符串表达式函数(我认为它被称为)

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."
Run Code Online (Sandbox Code Playgroud)

但是说我想要“和”之前的所有内容,并使用与我用来获取临时变量结果相同的方法,因此我想要

echo $temp = ".it has no purpose other than being here"
Run Code Online (Sandbox Code Playgroud)

我试过了

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

但这仅返回未更改的字符串内容

还有,手册页,这个叫什么,这个字符串表达式函数,在哪里可以找到更深入的信息?

cho*_*oba 15

您必须将星号放在字符串的另一端。星号代表“随便”,所以

${string%$searchstring*}
Run Code Online (Sandbox Code Playgroud)

意味着“从搜索字符串开始从字符串中删除所有内容”。

这记录在man bash“参数扩展”下。