Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ${}, # # and% in Shell

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use ${}, # # and%% in Shell". In daily operation, I believe many people have doubts about how to use ${}, # # and%% in Shell. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the questions of "how to use ${}, # # and%% in Shell"! Next, please follow the editor to study!

Suppose we define a variable as follows:

The code is as follows:

File=/dir1/dir2/dir3/my.file.txt

You can replace it with ${} to get different values:

The code is as follows:

${file#*/}: delete the first / and the string to its left: dir1/dir2/dir3/my.file.txt

${file##*/}: delete the last / and the string to its left: my.file.txt

${file#*.}: delete the first one. And the string to its left: file.txt

${file##*.}: delete the last one. And the string to its left: txt

${file%/*}: delete the last / and the string to the right: / dir1/dir2/dir3

${file%%/*}: delete the first / and the string to its right: (null)

${file%.*}: delete the last one. And the string to the right: / dir1/dir2/dir3/my.file

${file%%.*}: delete the first one. And the string to the right: / dir1/dir2/dir3/my

The method of memory is:

The code is as follows:

# is to remove the left (on the keyboard # to the left of $)

% is removed from the right side (% on the keyboard is on the right side of $)

A single symbol is a minimum match; two symbols are a maximum match.

${file:0:5}: extract the leftmost 5 bytes: / dir1

${file:5:5}: extract 5 consecutive bytes to the right of the fifth byte: / dir2

You can also replace the string in the value of the variable:

The code is as follows:

${file/dir/path}: replace the first dir with path:/path2/dir2/dir3/my.file.txt

${file//dir/path}: replace all dir with path:/path2/path3/path4/my.file.txt

Using ${}, you can also assign values for different variable states (no settings, null values, non-null values):

${file-my.file.txt}: if $file is not set, my.file.txt is used as the return value. (null and non-null values are not dealt with)

${file:-my.file.txt}: if $file is not configured or is a null value, my.file.txt is used as the return value. (not processed when non-null)

${file+my.file.txt}: if $file is set to null or non-null, my.file.txt is used as the return value. (no processing at setting time)

${file:+my.file.txt}: if $file is a non-null value, my.file.txt is used as the return value. (no processing when no settings and null values)

${file=my.file.txt}: if $file is not set, use my.file.txt as the return value and set $file to my.file.txt at the same time. (null and non-null values are not dealt with)

${file:=my.file.txt}: if $file is not set or is null, use my.file.txt as the return value and set $file to my.file.txt at the same time. (not processed when non-null)

${file?my.file.txt}: if $file is not configured, input my.file.txt to STDERR. (null and non-null values are not dealt with)

${file:?my.file.txt}: if $file is not set or is null, output my.file.txt to STDERR. (not processed when non-null)

${# var} calculates the length of the variable value:

${# file} gets 27 because / dir1/dir2/dir3/my.file.txt is 27 bytes

[/ code]

At this point, the study on "how to use ${}, # # and%% in Shell" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report