当前位置: 移动技术网 > 科技>操作系统>Linux > 【shell脚本】字符串和数组的使用

【shell脚本】字符串和数组的使用

2019年06月28日  | 移动技术网科技  | 我要评论

  字符串

可以使用单引号和双引号定义字符串变量但是单引号中不支持变量解析

#! /bin/bash
username="mayuan" str_1="hello ${username}" str_2='hello ${username}' echo $str_1 # hello mayuan echo $str_2 # hello ${username}

获取字符串的长度

#! /bin/bash
username="mayuan" echo ${#username} # 6

截取字符串

#! /bin/bash
username="mayuan" echo ${username:1:3} # 从第二个字符开始截取3个字符输出"ayu"

查找指定字符

#! /bin/bash
username="mayuan"
echo $(expr index "${username}" y)  #查找y在指定字符的位置

  数组

#! /bin/bash
arr=(1 2 3 4 5)
echo ${arr[0]} #输出1
echo ${arr[1]} #输出2

获取数组长度

#! /bin/bash
arr=(1 2 3 4 5)
echo ${#arr[@]} #输出5
echo ${#arr[*]}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网