当前位置:网站首页>Shell variable operation ${} detailed usage
Shell variable operation ${} detailed usage
2022-07-22 19:02:00 【Epiphyllum every month】
${} Basic function
In general $var And ${var} There is no difference , But with ${ } Will be more accurate to define the scope of variable name
[[email protected] ~]# A=Linux
[[email protected] ~]# echo $AB # Said variable AB
[[email protected] ~]# echo ${A}B # Said variable A Connected behind B
LinuxB
besides ${} There are many other functions as follows :
Declare test variables first :
file=/dir1/dir2/dir3/my.file.txt
Take substring and replace
command | explain | result |
---|---|---|
${file:0:5} | Extract the leftmost 5 Bytes | /dir1 |
${file:5:5} | To extract the first 5 The sequence to the right of a byte 5 Bytes | /dir2 |
${file/dir/path} | The first one. dir Replace it with path | /path1/dir2/dir3/my.file.txt |
${file//dir/path} | Will all dir Replace it with path | /path1/path2/path3/my.file.txt |
${#file} | Get the variable length | 27 |
Assign values to variables based on status
command | explain | remarks |
---|---|---|
${file-my.file.txt} | if $file No setting , Then use my.file.txt Return value | Set ( Null and non null values ) Do not deal with |
${file:-my.file.txt} | if $file Not set or null , Then use my.file.txt Return value | When the value is not null, it will not be processed |
${file+my.file.txt} | if $file Set ( Null or non null ), Both use my.file.txt Return value | If it is not set, it will not be processed |
${file:+my.file.txt} | if $file It is set and not null ( Is a non null value ), Then use my.file.txt Return value | If it is not set and null, it will not be processed |
${file=txt} | if $file No setting , Send it back txt , And will $file The assignment is txt | Set ( Null and non null values ) Do not deal with |
${file:=txt} | if $file No setting or null value , Send it back txt , take $file The assignment is txt | When the value is not null, it will not be processed |
${file?my.file.txt} | if $file No setting , Will my.file.txt Output to STDERR | Set ( Null and non null values ) Do not deal with |
${file:?my.file.txt} | if $file No setting or null value , Will my.file.txt Output to STDERR | When the value is not null, it will not be processed |
tips:
The above understanding lies in , You must distinguish unset And null And non-null These three assignment States . generally speaking , : And null of , If you don't bring : Words , null Unaffected , If : Zelian null Also affected .
my.file.txt Before “-” It can be understood as “ No definition , Replace with word”;“+” It can be understood as “ Defined , Replace with word”.
my.file.txt Before “?” It can be understood as “ Is the parameter defined , No definition , hold word When the error message is printed .”
${} String interception
Examples are as follows :
command | explain | result
${file#*.} Take off the first one . And the string to the left of it file.txt
[[email protected] ~]# echo ${file#*.}
file.txt
${file##*.} Take off the last one . And the string to the left of it txt
[[email protected] ~]# echo ${file##*.}
txt
${file%.*} Take off the last one . And the string to the right of it /dir1/dir2/dir3/my.file
[[email protected] ~]# echo ${file%.*}
/dir1/dir2/dir3/my.file
${file%%.*} Take off the first one . And the string to the right of it /dir1/dir2/dir3/my
[[email protected] ~]# echo ${file%%.*}
/dir1/dir2/dir3/my
The memory method is as follows :
- # Take out the left side ( On the keyboard # stay $ On the left )
- % Take out the right side ( On the keyboard % stay $ To the right )
- A single sign is the minimum match ; The two symbols are the maximum match
- * Is used to match unwanted characters , That is, the part you want to remove
- Also specify the character separator , And * coordination , Decide which part to take
The above is an online explanation , Follow the example above , As if ${} It can only be delimited with a single character , You can actually use string , And it is not only able to match one side of the deleted delimited string , In fact, both sides can be matched and deleted at the same time ( But because there is a line beginning or end delimitation ,# The pattern specifies that one side of the beginning of the line must be deleted when matching the beginning of the line ,% Pattern homology , See the explanation below )
Add : Test found , Actually, here # and % It refers to the beginning or end of the matching string , Specify to match ^ perhaps $. Successive # and $, Similar to regular greedy matching and shortest matching ,* Equivalent to .* , Similar to shell Match pattern in command . ${} In fact, it matches strings in this non-standard regular way , Then intercept and delete .
Examples are as follows :
${file##/*.fi}
The longest match , But it doesn't match the later "." , Because what matches here is "/*.fi",.fi And .txt Mismatch , And the front /*
Equivalent to regular ^/*, You can match the front part of the string .
[[email protected] ~]# echo ${file##/*.fi}
le.txt
If replaced {file##d*.}
, Because it matches the pattern at the beginning of the string , Ahead d* Equivalent to matching regular ^d*, If matching fails, the original string will be returned
[[email protected] ~]# echo ${file##d*.}
/dir1/dir2/dir3/my.file.txt
${file#*.t} The shortest match , The match here is ".*t", Can match .txt The front part , give the result as follows
[[email protected] ~]# echo ${file#*.*t}
xt
${file%dir*xt} Match the end of the line
[[email protected] ~]# echo ${file%dir*xt}
/dir1/dir2/
there * It's not necessary , No match can succeed
[[email protected] ~]# echo ${file%le.txt}
/dir1/dir2/dir3/my.fi
therefore ${} in # and $ It is to use a regular way to specify the match at the beginning or end of the line to match the deletion character
Array
A="a b c def" # Defining variables
A=(a b c def) # Define an array
command | explain | result |
---|---|---|
${A[@]} | Returns all elements of the array | a b c def |
${A[*]} | ditto | a b c def |
${A[0]} | Returns the first element of the array | a |
${#A[@]} | Returns the total number of array elements | 4 |
${#A[*]} | ditto | 4 |
${#A[3]} | Returns the length of the fourth element , namely def The length of | 3 |
A[3]=xzy | The fourth group number is redefined as xyz |
- In fact, shell Medium variable A And array elements A[0] It is equivalent. ( A string is an array with only one element )
After declaring variables separately , You can also take values in array
[[email protected] ~]# B="12 3 4"
[[email protected] ~]# echo ${B[*]}
12 3 4
[[email protected] ~]# echo ${#B[*]}
1
[[email protected] ~]# echo ${B[0]}
12 3 4
边栏推荐
- Leetcode 105. constructing binary trees from preorder and inorder traversal sequences
- Six dimensional space
- 逻辑回归中的损失函数
- BigDecimal中除法divide()方法的详细解析,带你走进源码的world
- Summary 20220211
- Learning to Incorporate Structure Knowledge for Image Inpainting
- 分库分表
- 实现各个微服务间限制IP访问 的三种方式
- Go language learning: go language journey (4)
- Strncpy() copy string (limited by length)
猜你喜欢
MySQL master-slave replication
Summary of all usage of join in SQL syntax (simple example)
[summary of linked list skills] 141. Circular linked list (simple)
Flink learning notes (VII) processing function
NRF24L01 wireless module setting transmit receive mode method
Caching-sha2-password problem occurred when connecting mysql8.0
1. The solution of line feed qt5- "vs" in constants; 2. Problems and solutions of common compilation of QT and vs of the same code
Leetcode 304. two dimensional area and retrieval - matrix immutable
Flink learning notes (V) datastream API
BigDecimal中除法divide()方法的详细解析,带你走进源码的world
随机推荐
程序员面试金典面试题 01.02. 判定是否互为字符重排
国内 Ngrok 实现内网穿透
Leetcode 114. expand binary tree into linked list
[summary of linked list skills] 141. Circular linked list (simple)
数据存储分区--范围分区,哈希分区,列表分区,性能调优必不可缺少的部分
Parameter index out of range (1 > number of parameters, which is 0).
代码—递归
Tcpdump 简单用法
Kindling the Darkness: A Practical Low-light Image Enhancer
1. Qtimer:: how to transfer parameters from singleshot, 2. Qmetaobject:: invokemethod how to transfer values with functions
JVM-JVM概述
写作单词积累
wget下载目录内的所有文件
Qt | 模态对话框和非模态对话框 QDialog
Leetcode 105. constructing binary trees from preorder and inorder traversal sequences
bjyx
Summary 20220209
How to resolve errors in executing the yum makecache command
paper - A Physics-based Noise Formation Model for Extreme Low-light Raw Denoising
Recursively find the partial sum of simple alternating power series (15 points)