This article is a mirror article of machine translation, please click here to jump to the original article.

View: 17244|Reply: 0

[linux] Linux Bash Shell String operation

[Copy link]
Posted on 2021-9-29 09:38:46 | | | |
${ 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:









Previous:Java generates a specified number of bits (numeric captchas)
Next:Linux environment basics: the difference between installing el6, el7, and el8 in rpm packages
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com