当前位置:网站首页>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 |
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 )
边栏推荐
- C language exercise 1
- [JS solution] leedcode 200 Number of islands
- Analysis on the usage, response and global delivery of provide/inject
- E - Lucky Numbers
- Kaggle 时间序列教程
- 【JS解决】leedcode 117. 填充每个节点的下一个右侧节点指针 II
- Createanonymousthreadx passes parameters to anonymous threads
- 工业互联网通用通信协议
- Your one-on-one meetings are inefficient. You can do this!
- Four methods for judging JS data types and user-defined methods
猜你喜欢
Advantages of win8.1 and win10
Embedded hardware - read schematic
[JS solution] leedcode 200 Number of islands
How to use redis
How to lay copper in AD (aluminum designer)
Draw a hammer
无限循环滚动代码阿里巴巴国际站店铺装修代码底图滚动黑色半透明显示效果自定义内容装修代码全屏显示
RMQ、LCA
Colab tutorial (super detailed version) and colab pro/pro+ evaluation
Crawler scrapy framework learning 1
随机推荐
Go/golang connection to database
2022 question bank and answers for operation certificate examination of safety production management personnel in road transport enterprises
Conception d'un système basé sur MVC avec javaswing JDBC
2022 ICML | Pocket2Mol: Efficient Molecular Sampling Based on 3D Protein Pockets
自动评教脚本使用的配置
Section 5 - Operator details
Mind mapping series - Database
josephus problem
Develop go using vscode
约瑟夫问题
Analysis on the usage, response and global delivery of provide/inject
Force buckle 92 Reverse linked list II
2021tami/ image processing: exploiting deep generative priority for versatile image restoration and manipulation
C盘无损移动文件
Colab使用教程(超级详细版)及Colab Pro/Pro+评测
QT client development -- driver loading problem of connecting to MySQL database
Blockly learning ----2 Code generation, grid, scaling, events, storage
Colab tutorial (super detailed version) and colab pro/pro+ evaluation
[untitled]
Section 7 - structures