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

Use of ${}, # # and% in shell

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Suppose we define a variable as follows:

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

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

${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:

# 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:

${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

Transferred from: http://space.baidu.com.cn/ugo5/blog/item/c550bbc54d1644079c163dbd.html

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

Servers

Wechat

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

12
Report