SoFunction
Updated on 2025-03-09

Example of a Linux Shell method to intercept strings

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 illustrate
${string: start :length} Start from the start character on the left of the string string and intercept the length character to the right.
${string: start} Start from the start character on the left side of the string string until the end.
${string: 0-start :length} Start from the start character on the right of the string string and intercept the length character to the right.
${string: 0-start} Start from the start character on the right side of the string string until the end.
${string#*chars} Starting from the position where *chars first appears in the string string, intercept all characters to the right of *chars.
${string##*chars} Starting from the position where *chars last appears in the string string, intercept all characters to the right of *chars.
${string%*chars} Starting from the position where *chars first appears in the string string, intercept all characters to the left of *chars.
${string%%*chars} Starting from the position where *chars last appears in the string string, intercept all characters to the left of *chars.

--------------------------------------------------------------------------------

Here are a few examples to show:

1) Get the length of the string

grammar:

${#var}

Sample code:

str="./blog/shell-truncating-string"
echo "string : [${str}]"

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

Execution results:

string : [./blog/shell-truncating-string]
length : [61]

--------------------------------------------------------------------------------

2) Use # and ## to get the tail substring

2.1) # Minimum number of word intercepted from the front

grammar:

${parameter#word} 

Sample code:

str="./blog/shell-truncating-string"
echo "string : [${str}]"

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

Execution results:

string : [./blog/shell-truncating-string]
substr : [/./blog/shell-truncating-string]

2.2) ## Maximum intercept word from the front

grammar:

${parameter##word}

Sample code:

str="./blog/shell-truncating-string"
echo "string : [${str}]"

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

Execution results:

string : [./blog/shell-truncating-string]
substr : [shell-truncating-string]

--------------------------------------------------------------------------------

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

3.1) % Minimum number of word intercepted from behind

grammar:

${parameter%word} 

Sample code:

str="./blog/shell-truncating-string"
echo "string : [${str}]"

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

Execution results:

string : [./blog/shell-truncating-string]
substr : [./blog]

3.2) %% Maximum word intercepts from the back

grammar:

${parameter%%word}

Sample code:

str="./blog/shell-truncating-string"
echo "string : [${str}]"

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

Execution results:

string : [./blog/shell-truncating-string]
substr : [http:]

--------------------------------------------------------------------------------

4) Use ${var:} mode to get substrings

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

grammar:

${var:start:len}

Sample code:

str="./blog/shell-truncating-string"
echo "string : [${str}]"

#The 0 indicates the start of the first character on the left, and 7 indicates the total number of sub-characters.substr=${str:0:7}
echo "substr : [${substr}]"

Execution results:

string : [./blog/shell-truncating-string]
substr : [http://]

4.2) Start from the left character until the end

grammar:

${var:7}

Sample code:

str="./blog/shell-truncating-string"
echo "string : [${str}]"

#The 7 of them means the 8th character on the left startssubstr=${str:7}
echo "substr : [${substr}]"

Execution results:

string : [./blog/shell-truncating-string]
substr : [./blog/shell-truncating-string]

4.3) Start from the right character and the number of characters

grammar:

${var:0-start:len}

Sample code:

str="./blog/shell-truncating-string"
echo "string : [${str}]"

#The 0-23 of them means that the 23rd character is counted on the right, and 5 means the number of characterssubstr=${str:0-23:5}
echo "substr : [${substr}]"

Execution results:

string : [./blog/shell-truncating-string]
substr : [shell]

4.4) Start from the right character until the end

grammar:

${var:0-start}

Sample code:

 

str="./blog/shell-truncating-string"
echo "string : [${str}]"

#The 0-6 means that the 6th character is counted on the rightsubstr=${str:0-6}
echo "substr : [${substr}]"

Execution results:

string : [./blog/shell-truncating-string]
substr : [string]

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.