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

String processing of shell

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

String slicing:

${var:offset:number}

Offset: the number of strings to skip

Number: the number of strings to be fetched

Example:

Name= "Obama"

${name:1:2}-- > shows that the result is ba

${name:1}-- > shows that the result is bama

Take the rightmost characters of the string: ${var:-legh}:

Note: there must be a white space character after the colon

# echo ${name:-3}

Get the substring based on the pattern:

Match from left to right

${var#*word}: where word can be any character specified; function: from left to right, find the word (character) in the string stored by the var variable, and delete all characters between the beginning of the string and the first occurrence of the word character (including the word character)

${var##*word}: same as above, however, everything between the beginning of the string and the last character referred to by word is deleted

Match from right to left

${var%word*}: where word can be any character specified; function: from left to right, find the word (character) that appears for the first time in the string stored in the var variable, and delete all characters between the last character left and the first occurrence of the word character.

${var%%word*:}: same as above, however, everything from the end of the string to the character of the first word reference word is deleted

Example: usr= http://www.magedu.com:80

# ${usr##*:}: 80 is fetched

# ${usr%%:*}: the one that goes most is http

Find and replace:

${var/pattern/substi}:

Find the string represented by var that is matched by pattern for the first time and replace it with substi

# user=$ (head-1 / etc/passwd)

# echo ${user/root/ROOT}

${var//pattern/substi}:

Find all the strings represented by var that can be matched by pattern and replace them with substi

${var/#pattern/substi}:

Look for the string represented by var, and replace it with substi after the beginning of the line is matched by pattern

${var/%pattern/substi}:

Find the string represented by var, the string to which the end of the line is matched by pattern, and replace it with substi

Find and delete:

${var/pattern}: find the string represented by var and delete it if it is matched for the first time

${var//pattern}: find all matches in the string represented by var for deletion

${var/#pattern}: find the string represented by var and delete the line to which the beginning of the line is matched

${var/%pattern}: find the string represented by var where the end of the line is matched for deletion

Character case conversion:

${var^ ^}: converts all lowercase letters in var to uppercase:

${var,}: converts all uppercase letters in var to lowercase:

Variable assignment:

${var:-value}: if var is empty or not set, return value;. Otherwise, return the value of var.

${var:=value}: if var is empty or not set, assign value to var and return (equal to setting default)

${var:+value}: if the var is different, the return value; is exactly the opposite of ${var:-value}

${var:?error_info}: if var is empty or not set, return error_info;. Otherwise, return the value of var.

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

*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

Wechat

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

12
Report