您的位置:宽带测速网 > 编程知识 > shell怎么查找字符串位置

shell怎么查找字符串位置

2025-06-21 20:32来源:互联网 [ ]

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

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

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