# 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
str
ORdefaultVal
${str-defaultVal}
: Ifstr
is undefined, returndefaultVal
.${str:-defaultVal}
: Ifstr
is 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 defaultVal
Set and return
defaultVal
,OR returnstr
.(位置参数或特殊参数不能以这种方式赋值)${str=defaultVal}
: Ifstr
is undefined, setstr=defaultVal
and returndefaultVal
.${str:=defaultVal}
: Ifstr
is undefined or empty, setstr=defaultVal
and 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
newDefineVal
ORempty
${str+newDefinedVal}
: Ifstr
is defined, returnnewDefinedVal
,else return empty.${str:+notEmptyVal}
: Ifstr
is 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
error
message ORstr
${str?ERR_MSG}
: Ifstr
is undefined printERR_MSG
, else returnstr
.${str:?ERR_MSG}
: Ifstr
is 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 7
Substring.
${str:position}
:substring start atposition
.${str:position:length}
:substring oflength
start atposition
.${str#substr}
:Remove the shortest matchingsubstr
at the beginning ofstr
${str##substr}
:Remove the longest matchingsubstr
at the beginning ofstr
.${str%substr}
:Remove the shortest matchingsubstr
at the end ofstr
.${str%%substr}
:Remove the longest matchingsubstr
at 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 aa
Replace
${str/oldstr/newstr}
:replaceold
str once withnewstr
.${str//oldstr/newstr}
:replace alloldstr
withnewstr
.${str/#oldstr/newstr}
:replaceoldstr
withnewstr
at the beginning.${str/%oldstr/newstr}
:replaceoldstr
withnewstr
at 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实用技巧 →