![endif]-->!-->![endif]-->!-->![endif]-->!-->
Bash基本字符串操作
一、取得字符串长度
${#string} |
二、得到子字符串的位置
` expr index “$str” “$substr” ` |
三、取得字符串的子串
` expr ${str:3} ` ` expr ${str:3:3} ` ` expr substr “$str” 3 ` ` expr substr “$str” 3 3 ` |
四、替换字符串
` expr ${str/oldsubstr/newsubstr} ` ` expr ${str//oldsubstr/newsubstr} ` |
五、代码
#! /bin/bash
stringZ=abcdefghijklmnop
######Get string length echo "Output stringZ length:" echo ${#stringZ} ##result #Output stringZ length: #16
#######Get substring position echo "def is at the index:" echo `expr index "$stringZ" "def"`
substr=mno echo `expr index "$stringZ" "$substr"` ##result def is at the index: 4 13
#########Get substing echo `expr ${stringZ:3}` echo `expr ${stringZ:3:5}` #echo `expr substr "$stringZ" 2 ` echo `expr substr "$stringZ" 2 3 ` ###result #defghijklmnop #defgh #bcd
##########Replace substring stringZ=abcdefgabc echo `expr ${stringZ/abc/ABC}` echo `expr ${stringZ//abc/ABC}` ###result #ABCdefgabc #ABCdefgABC |