To test and replace variables in shell, we often need to determine whether a variable exists at some time point. if a variable exists, use the existing settings. if the variable does not exist, give a common setting. (1) If the variable is not set or the content is empty, replace it with the new content. new_var $ {o... in the information shell, we often need to determine whether a variable exists at some time points for testing and replacement of variables. if a variable exists, use the existing settings. if the variable does not exist, give a common setting. (1) If the variable is not set or the content is empty, replace it with the new content. new_var = $ {old_var-content} [html] Example 1: test whether there is a user name, if not, give username content as root [root @ www ~] # Echo $ username <= because there is a blank space, the username may not exist or it may be a null string [root @ www ~] # Username =$ {username-root} [root @ www ~] # Echo $ username root <= because username is not set, the content is given as root. [Root @ www ~] # Username = "vbird tsai" [root @ www ~] # Username =$ {username-root} [root @ www ~] # Echo $ username vbird tsai <= because username has been set, so use old? In the preceding example, if username is set as an empty string, it will not be replaced. if you want to replace it for the next time, you can add ":", that is, # username =$ {username:-root} [html] Example 2: If username is not set or is an empty string, set the username content to root [root @ www ~] # Username = "" [root @ www ~] # Username =$ {username-root} [root @ www ~] # Echo $ username <= because username is set as an empty string! So of course, it is still a null string! [Root @ www ~] # Username =$ {username:-root} [root @ www ~] # Echo $ username root <= after ":" is added, can it be used later if the sent content is empty or not set? Content replacement. (2) "-" does not affect the old content. to replace the new content, use "=" [html] [root @ www ~]. # Unset str; var =$ {str = newvar} [root @ www ~] # Echo var = "$ var", str = "$ str" var = newvar, str = newvar <= because str does not exist, var/str are all newvar tests: if str already exists, what will var do if it is tested? [Root @ www ~] # Str = "oldvar"; var =$ {str = newvar} [root @ www ~] # Echo var = "$ var", str = "$ str" var = oldvar, str = oldvar <= because str exists, var is equal to str content (3) if I want to know, if the old sending volume does not exist, the whole test will tell me that there is a "wrong token". now I can use the question mark [html] [root @ www ~] # Unset str; var =$ {str? None of the variables}-bash: str: none of the sending volumes <= because str does not exist, the output error message (4) summary
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.