美文网首页
shell之字符串

shell之字符串

作者: IT小池 | 来源:发表于2019-06-18 20:29 被阅读0次
    获取字符串长度:${#变量}expr length 变量
    [root@localhost /]# var1='test'
    [root@localhost /]# echo var1
    var1
    [root@localhost /]# echo ${#var1}
    4
    [root@localhost /]# echo `expr length "$var1"`
    4
    
    获取字符串索引位置:expr index变量 获取字符
    [root@localhost /]# var1='test'
    [root@localhost /]# echo `expr index "$var1" e`
    2
    [root@localhost /]# 
    
    获取子串长度:expr match 变量 待匹配字符
    [root@localhost /]# var1='test'
    [root@localhost /]# echo `expr match "$var1" te`
    2
    [root@localhost /]# 
    

    注意:必须是从头开始匹配

    抽取字符串中的子串:
    1. ${string: position}${string: position:length}${string: -position}${string: (position)}
    2. expr substr $string $position $length
      注意:使用 expr ,索引是从 1 开始计算,而使用 ${string:position,索引是从 0 开始}

    相关文章

      网友评论

          本文标题:shell之字符串

          本文链接:https://www.haomeiwen.com/subject/ulbqqctx.html