获取字符串长度
str=Alan
echo ${#str}
# 4
最小限度从前面截取字符串
str=https://www.runoob.com/linux/linux-shell-passing-arguments.html
echo ${str#*/}
# /www.runoob.com/linux/linux-shell-passing-arguments.html
最大限度从前面截取字符串
str=https://www.runoob.com/linux/linux-shell-passing-arguments.html
echo ${str##*/}
# linux-shell-passing-arguments.html
最小限度从后面截取字符串
str=https://www.runoob.com/linux/linux-shell-passing-arguments.html
echo ${str%/*}
# https://www.runoob.com/linux
最大限度从后面截取字符串
str=https://www.runoob.com/linux/linux-shell-passing-arguments.html
echo ${str%%/*}
# https:
使用${var:} 模式获取子字符串
语法:
${var:start:len}
str=https://www.runoob.com/linux/linux-shell-passing-arguments.html
echo ${str:0:6}
# https:
从左边第几个字符开始一直到结束
语法:
${var:start}
str=https://www.runoob.com/linux/linux-shell-passing-arguments.html
echo ${str:6}
# //www.runoob.com/linux/linux-shell-passing-arguments.html
从右边第几个字符开始以及字符的个数
语法:
${var:0-start:len}
str=https://www.runoob.com/linux/linux-shell-passing-arguments.html
echo ${str:0-15:5}
# -argu
从右边第几个字符开始一直到结束
语法:
${var:0-start}
str=https://www.runoob.com/linux/linux-shell-passing-arguments.html
echo ${str:0-15}
# -arguments.html