An example of how Linux shell intercepts strings

Time:2019-12-1

There are many ways to intercept strings in shell


${var#*/}
${var##*/}
${var%/*}
${var%%/*}
${var:start:len}
${var:start}
${var:0-start:len}
${var:0-start}
format Explain
${string: start :length} Start with the first character on the left of the string and intercept the length character to the right.
${string: start} It starts from the first character to the left of the string and ends.
${string: 0-start :length} Start with the first character to the right of the string, and intercept the length character to the right.
${string: 0-start} It starts from the first character to the right of the string and ends.
${string#*chars} All characters to the right of * chars are truncated from the first occurrence of * chars in a string.
${string##*chars} All characters to the right of * chars are truncated from the last occurrence of * chars in string.
${string%*chars} All characters to the left of * chars are truncated from the first occurrence of * chars in a string.
${string%%*chars} All characters to the left of * chars are truncated from the last occurrence of * chars in string.

——————————————————————————–

Here are a few examples:

1) get the length of the string

Syntax:


${#var}

Example code:


str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]"

length=${#str}
echo "length : [${length}]"

Execution result:


string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
length : [61]

——————————————————————————–

2) use ා, and ා, to get the tail substring

2.1) at least intercept word from the front

Syntax:


${parameter#word} 

Example code:

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]"

#The separator is' / '
substr=${str#*/}
echo "substr : [${substr}]"

Execution result:


string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [/www.fengbohello.xin3e.com/blog/shell-truncating-string]

2.2) cut word from the front as much as possible

Syntax:


${parameter##word}

Example code:

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]"

#The separator is' / '
substr=${str##*/}
echo "substr : [${substr}]"

Execution result:


string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [shell-truncating-string]

——————————————————————————–

3) use% and%% to get the header substring

3.1) cut word from the back as a minimum

Syntax:


${parameter%word} 

Example code:


str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]"

substr=${str%/*}
echo "substr : [${substr}]"

Execution result:


string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [http://www.fengbohello.xin3e.com/blog]

3.2)%% intercept word from the back as much as possible

Syntax:


${parameter%%word}

Example code:


str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]"

substr=${str%%/*}
echo "substr : [${substr}]"

Execution result:


string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [http:]

——————————————————————————–

4) use ${var:} mode to get substring

4.1) specify the number of characters starting from the left and in the substring

Syntax:


${var:start:len}

Example code:

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]"

#Where 0 represents the beginning of the first character on the left and 7 represents the total number of sub characters.
substr=${str:0:7}
echo "substr : [${substr}]"

Execution result:


string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [http://]

4.2) from the first few characters on the left to the end

Syntax:


${var:7}

Example code:

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]"

#Where 7 indicates the beginning of the eighth character on the left
substr=${str:7}
echo "substr : [${substr}]"

Execution result:


string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [www.fengbohello.xin3e.com/blog/shell-truncating-string]

4.3) starting from the character on the right and the number of characters

Syntax:


${var:0-start:len}

Example code:

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]"

#Where 0-23 represents the beginning of the 23rd character from the right, and 5 represents the number of characters
substr=${str:0-23:5}
echo "substr : [${substr}]"

Execution result:


string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [shell]

4.4) from the first few characters on the right to the end

Syntax:


${var:0-start}

Example code:

 

str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [${str}]"

#Where 0-6 means the sixth character from the right
substr=${str:0-6}
echo "substr : [${substr}]"

Execution result:


string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [string]

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.