温馨提示×

shell怎么查找字符串位置

小亿
130
2024-03-26 10:09:41
栏目: 编程语言

在shell中查找字符串位置可以使用expr命令结合index函数来实现。以下是一个示例:

string="hello world"
substring="world"

position=$(expr index "$string" "$substring")
echo "Substring '$substring' found at position $position"

在这个示例中,string是要查找的字符串,substring是要查找的子字符串。expr index命令会返回子字符串在字符串中第一次出现的位置。

0