${ varname :- word }: If varname exists and is not null, returns the value of varname, otherwise returns word. Used to return a default value when a variable does not exist or is not defined. If it is ${ varname - word }, it can be null when the varname exists, e.g. we define myparam=, returning the value of the varname, otherwise returning the word. You can also delete ":", which has the same meaning and is not repeated.
${ varname := word }: If varname exists and is not null, returns the value of varname, otherwise assigns it a value of word and returns that value. Used to set a default for a variable that does not exist or is not defined. For position parameters, read-only and cannot be assigned.
${ varname :? message }: If varname exists and is not null, returns the value of varname, otherwise prints the information varname:message, and abort script, if we don't set message, the default "parameter null or not set" will be used. Used to check for errors where variables are not defined.
${ varname :+ word }: If varname exists and is not null, returns the value of word, otherwise it returns null. Used to detect the presence or absence of variables.
${ varname : offset : length }: Operation to get the substring. He returns a string of length from the offset position in the varname. The first position is 0, if length is not given or is invalid (less than 0), all characters after offset are returned, and if it is {@:offset:length}, the total length parameter of the script parameter from the first offset parameter is returned.
Create a new test.sh test script as follows:
|