# 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

  1. Normal use, return str

    • ${str}: same as $str.
    echo "$str1";
    echo "${str1}";
    echo $str1; 
    echo ${str1};
    #位置参数大于9的,一定要加花括号,例如第十个位置参数:
    ${11}
    
  2. Return str OR defaultVal

    • ${str-defaultVal}: If str is undefined, return defaultVal.
    • ${str:-defaultVal}: If str is undefined or empty, return defaultVal.
    # 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
    
  3. Set and return defaultVal ,OR return str.(位置参数或特殊参数不能以这种方式赋值)

    • ${str=defaultVal}: If str is undefined, set str=defaultVal and return defaultVal.
    • ${str:=defaultVal}: If str is undefined or empty, set str=defaultVal and return defaultVal.
    # 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.
    
  4. Return newDefineVal OR empty

    • ${str+newDefinedVal}: If str is defined, return newDefinedVal,else return empty.
    • ${str:+notEmptyVal}: If str is defined and is not empty,return notEmptyVal.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)
    
  5. Print error message OR str

    • ${str?ERR_MSG}: If str is undefined print ERR_MSG, else return str.
    • ${str:?ERR_MSG}: If str is undefined or empty, print ERR_MSG.else return str.
    #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"
    
  6. Match prefix

    • ${!strPrefix*}:Matches all variables starting with strPrefix.
    • ${!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)

  1. Length.

    • ${#str}:Length of string.
    #str undefined
    echo ${#str}; #output 0
    #str defined
    str="abcdefg";
    echo ${#str}; #output 7
    
  2. Substring.

    • ${str:position}:substring start at position.
    • ${str:position:length}:substring of length start at position.
    • ${str#substr}:Remove the shortest matching substr at the beginning of str
    • ${str##substr}:Remove the longest matching substr at the beginning of str.
    • ${str%substr}:Remove the shortest matching substr at the end of str .
    • ${str%%substr}:Remove the longest matching substr at the end of str.

    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
    
  3. Replace

    • ${str/oldstr/newstr}:replace oldstr once with newstr.
    • ${str//oldstr/newstr}:replace all oldstr with newstr.
    • ${str/#oldstr/newstr}:replace oldstr with newstr at the beginning.
    • ${str/%oldstr/newstr}:replace oldstr with newstr 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-->>