当前位置:网站首页>Shell built-in string substitution

Shell built-in string substitution

2022-06-13 04:53:00 qq_ thirty-nine million two hundred and eighty thousand seven h

shell Variable assignment syntax : 

Usage rule explain
Single quotation marks What you see is what you get , That is, all contents within single quotation marks will be output as is , Or it is described as what you see in single quotation marks and what will be output , This is called strong reference
Double quotes ( Default ) Output everything in double quotes ; If there is an order in the content ( It's going to lead you down )、 Variable 、 Special escape characters, etc , It's going to put variables first 、 command 、 The escape character parses the result , And then output the final content , Recommended , This is called weak reference
No quotes assignment , If there are spaces in the variable content , The assignment will be incomplete . And when you output content , The string containing spaces will be output as a whole ; If there is an order in the content ( It's going to lead you down )、 Variable etc. , Then the variable will be 、 The command parses the result , And then output the final content ; If there are special characters such as spaces in the string , You may not be able to completely output , Therefore, double quotation marks are required to replace the case without quotation marks , In particular, double quotation marks are added when assigning values to variables . Generally continuous string 、 Numbers 、 The path can be assigned and output without any quotation marks , But it's better to use it
The quotation marks Generally used to reference commands , When executed, the command will be executed , amount to S0, Both assignment and output use “ Cause an order to

quote :shell Define the difference between double quotation marks, single quotation marks and no quotation marks for variables in

Examples of use without quotation marks :

  Assignment without quotation marks , Contain Spaces :

 

take world Interpreted as a variable  

Assignment without quotation marks , Variables contain spaces :

Normal output :

Use summary without quotation marks :
assignment , Do not insert special characters such as spaces .

shell Built in String Syntax :

         Writing shell The program , String related operations are often involved . There are many command statements , Such as awk,sed Can do a variety of string operations . in fact shell Built in a series of operation symbols , Similar results can be achieved , Using the internal operator will omit the time to start the external program , So the speed will be very fast . If the built-in operators can do it , Use built-in... First .

quote :shell String built-in common operations

1 Read string value

expression meaning
${var} Variable var The original value
${var-default}var No return is declared default, But it doesn't change. var Value
${var:-default}var If it is not declared or its value is null, return default, But it doesn't change. var Value
${var=default}var No declaration to return default, And will var Is set to default
${var:=default}var If it is not declared or its value is null, return default, And will var Is set to default
${var+other}var Declared return other, But it doesn't change. var Value
${var:+other}var Declared and not empty return other, But it doesn't change. var Value
${var?err_msg}var Not declared , The message is err_msg Send to standard error output
${var:?err_msg}var Not declared or empty , The message is err_msg Send to standard error output
${!varprefix*} Before matching, all are marked with varprefix Variables declared at the beginning
${[email protected]} Before matching, all are marked with varprefix Variables declared at the beginning

2 String manipulation

expression meaning
${#string}string The length of
${string:position} stay string in , From the position position Start extracting substrings
${string:position:length} stay string in , From the position position Start extraction with a length of $length The string of
${string#substring} Dependent variable string The beginning of , Delete the shortest match substring The string of
${string##substring} Dependent variable string The beginning of , Delete the longest match substring The string of
${string%substring} Dependent variable string Ending , Delete the shortest match substring The string of
${string%%substring} Dependent variable string Ending , Delete the longest match substring The string of
${string/substring/replacement} Use replacement, To replace the first matching substring
${string//substring/replacement} Use replacement, Replace all matching substring
${string/#substring/replacement} hypothesis string The prefixes match substring, Then use replacement To replace the matching substring
${string/%substring/replacement} hypothesis string The suffix matches substring, Then use replacement To replace the matching substring
  explain :"substring” It can be a regular expression

shell Examples of built-in strings :

1. String substitution

${string/substring/replacement}
string It's a shell Variable (${} The rules )

原网站

版权声明
本文为[qq_ thirty-nine million two hundred and eighty thousand seven h]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280518242293.html