# Linux String and Digital字符串和数字
# String definition
#Global variable define
str1=abc;
str2="abc";
str3='abc';
#local variable define
fn1() {
local local_str1=abc;
local local_str2="abc";
local local_str3='abc';
}
function fn1 {
local local_str1=abc;
local local_str2="abc";
local local_str3='abc';
}
# Use String Variable
Normal use, return
str${str}: same as$str.
echo "$str1"; echo "${str1}"; echo $str1; echo ${str1}; #位置参数大于9的,一定要加花括号,例如第十个位置参数: ${11}Return
strORdefaultVal${str-defaultVal}: Ifstris undefined, returndefaultVal.${str:-defaultVal}: Ifstris undefined or empty, returndefaultVal.
# str undefined echo ${str-defaultVal}; #output defaultVal echo ${str:-defaultVal}; #output defaultVal echo ${str}; # nothing # str defined str="abc"; echo ${str-defaultVal}; #output abc echo ${str:-defaultVal}; #output abc echo ${str}; # abc # different(str is empty) str=; echo ${str-defaultVal}; #output ''(empty) echo ${str:-defaultVal}; # output defaultValSet and return
defaultVal,OR returnstr.(位置参数或特殊参数不能以这种方式赋值)${str=defaultVal}: Ifstris undefined, setstr=defaultValand returndefaultVal.${str:=defaultVal}: Ifstris undefined or empty, setstr=defaultValand returndefaultVal.
# str undefined echo ${str=defaultVal}; #output defaultVal and set str=defaultVal echo ${str:=defaultVal}; #output defaultVal and set str=defaultVal echo ${str}; # output defaultVal #str defined str="abc"; echo ${str=defaultVal}; # output abc. echo ${str:=defaultVal}; # output abc. echo ${str}; # output abc #different(str is empty) str=; echo ${str=defaultVal}; # output ''(empty) echo ${str:=defaultVal}; # output defaultVal.Return
newDefineValORempty${str+newDefinedVal}: Ifstris defined, returnnewDefinedVal,else return empty.${str:+notEmptyVal}: Ifstris defined and is not empty,returnnotEmptyVal.else return empty.
# str undefined echo ${str+newDefineVal}; #output ''(empty) echo ${str:+newDefineVal}; #output ''(empty) #str defined str="abc"; echo ${str+newDefinedVal}; # output newDefinedVal echo ${str:+newDefinedVal}; # output newDefinedVal #different(str is empty) str=; echo ${str+newDefinedVal};# output newDefinedVal echo ${str:+newDefinedVal};# output ''(empty)Print
errormessage ORstr${str?ERR_MSG}: Ifstris undefined printERR_MSG, else returnstr.${str:?ERR_MSG}: Ifstris undefined or empty, printERR_MSG.else returnstr.
#str undefined echo ${str?undefined}; #output "-bash: stsfdr: undefined" echo ${str:?undefineOrEmpty}; #output "-bash: stssr: undefineOrEmpty" #str defined str="abc"; echo ${str?undefined}; #output abc echo ${str:?undefinedorEmpty}; # output abc # different (str is empty) str=; echo ${str?undefined}; #output ''(empty) echo ${str:?undefinedOrEmpty} # output "-bash: stssr: undefineOrEmpty"Match prefix
${!strPrefix*}:Matches all variables starting withstrPrefix.${!strPrefix@}: The same as above.
#str undefined echo ${!str*}; # output ''(empty) #str defined str=abc str1=abc1; str2=abk2; str3=abe3; str4=abd4; e${!str*}; # output 'str str1 str2 str3 str4'
# String operation(length, substr, replace)
Length.
${#str}:Length of string.
#str undefined echo ${#str}; #output 0 #str defined str="abcdefg"; echo ${#str}; #output 7Substring.
${str:position}:substring start atposition.${str:position:length}:substring oflengthstart atposition.${str#substr}:Remove the shortest matchingsubstrat the beginning ofstr${str##substr}:Remove the longest matchingsubstrat the beginning ofstr.${str%substr}:Remove the shortest matchingsubstrat the end ofstr.${str%%substr}:Remove the longest matchingsubstrat the end ofstr.
attention:
- substr support "*" wildcard.
- shortest and longest is working at "*" wildcard.
# If str undefined, All of them return ''(empty) #str defined str="aabbcckkaabbkk"; echo ${str:0}; #output the whole str, aabbcckkaabbkk echo ${str:3}; #output bcckkaabbkk echo ${str:3:2}; #output bc echo ${str#aab}; # output bcckkaabbkk echo ${str#*b}; # output bcckkaabbkk echo ${str##aab}; # output bcckkaabbkk echo ${str##*b}; #output kk echo ${str%bkk}; #output aabbcckkaab echo ${str%b*}; #output aabbcckkaab echo ${str%%bkk}; #output aabbcckkaab echo ${str%%b*}; #output aaReplace
${str/oldstr/newstr}:replaceoldstr once withnewstr.${str//oldstr/newstr}:replace alloldstrwithnewstr.${str/#oldstr/newstr}:replaceoldstrwithnewstrat the beginning.${str/%oldstr/newstr}:replaceoldstrwithnewstrat the end.
attention:
- oldstr support "*" wildcard.
str1="aabbddaakkdd"; ##replace echo "${str1/aa/111}"; //replace:111bbddaakkdd echo "${str1//aa/111}"; //replaceAll: 111bbdd111kkdd echo "${str1/#aa/111}"; //replace begin: 111bbddaakkdd echo "${str1/#bb/111}"; //replace begin(没匹配上): aabbddaakkdd echo "${str1/%dd/111}"; //replace end:aabbddaakk111 echo "${str1/%aa/111}"; //replace end(没匹配上):aabbddaakkdd
key words: linux字符串,字符串定义,字符串替换,字符串截取。
<<---end-->>
← CentOS登录日志 Vim实用技巧 →