shell字符串索引

Posted Sawyer Ford

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell字符串索引相关的知识,希望对你有一定的参考价值。

shell中的字符串索引一会从0开始,一会从1开始,见例子:

#!/bin/bash

string="hello world"
length=${#string}
echo "string is \\""$string"\\""
echo "length is $length"

index=`expr index "$string" \'o\'`
echo "index of \'o\' is $index"

substring=${string:$index}
echo "substring begins from index $index is \\""$substring"\\""
echo "shit happens"
echo

exit 0

输出如下:

以上是关于shell字符串索引的主要内容,如果未能解决你的问题,请参考以下文章