shell intercept variable

Suppose there is variable var=http://www.aaa.com/123.htm.

1. Intercept the # sign, delete the characters on the left, and keep the characters on the right.

1
echo ${var#* //}

 Where var is the variable name, the # sign is the operator, *// means to delete the first // sign and all the characters on the left from the left,
that is, delete http://
and the result is: www.aaa.com/123.htm

2. The ## sign is intercepted, the characters on the left are deleted, and the characters on the right are reserved.

1
echo ${var##*/}

 

##*/ means to delete the last (rightmost) / sign from the left and all the characters on the left to
delete http://www.aaa.com/

The result is 123.htm

3. % sign interception, delete the right character, keep the left character

1
echo ${var%/*}

 

%/* means start from the right, delete the first / sign and the characters to the right

The result is: http://www.aaa.com

4. %% sign interception, delete the characters on the right, keep the characters on the left

1
echo ${var%%/*}

 %%/* means start from the right, delete the last (leftmost) / sign and the characters on the right. The
result is: http:

5. The first character from the left, and the number of characters

1
echo ${var: 0 : 5 }

 

Where 0 indicates the start of the first character on the left, and 5 indicates the total number of characters.
The result is: http:

6. Start with the first few characters from the left and continue to the end.

1
echo ${var: 7 }

 

The 7 represents the beginning of the 8th character from the left and continues until the end.
The result is: www.aaa.com/123.htm

7. The first few characters from the right, and the number of characters

1
echo ${var: 0 - 7 : 3 }

 

Among them, 0-7 means the seventh character from the right, and 3 means the number of characters.
The result is: 123

8. Start with the first few characters from the right and continue until the end.

1
echo ${var: 0 - 7 }

 

Indicates from the seventh character from the right to the end.
The result is: 123.htm

Note: (the first character on the left is represented by 0, and the first character on the right is represented by 0-1)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326209232&siteId=291194637