Let's take a look at an example:
#!/bin/basharea2=(zero one two three four)echo "Origin is :"echo ${area2[0]} ${area2[1]} ${area2[2]} ${area2[3]} ${area2[4]}area2[1]=1area2[4]=4echo "After is :"echo ${area2[0]} ${area2[1]} ${area2[2]} ${area2[3]} ${area2[4]}echo ${#area2}echo "One way to output the array:"echo ${area2[@]}
Output:
Root @ vivi-Ideapad-Z460 :~ #./Myshell. Sh
Origin is:
Zero One two three four
After is:
Zero 1 two three 4
4
One way to output the array:
Zero 1 two three 4
Root @ vivi-Ideapad-Z460 :~ #
#! /Bin/basharea2 = (zero one two three four) echo "origin is:" Echo ${area2 [@]} echo ${area2 [@]: 0} # extract the substring echo ${area2 [@]: 1} echo ${area2 [@]: 1: 2}
Root @ vivi-Ideapad-Z460 :~ #./Myshell. Sh
Origin is:
Zero One two three four
Zero One two three four
One two three four
One Two
Root @ vivi-
#
Delete a substring
#
Removes the shortest match from the front of the string,
# + The matching string is a regular expression.
Echo $ {arrayz [@] # f * r} # One Two Three five
#
The matching expression acts on all elements of the array.
#
Match "four" and delete it.
# Longest match at the front of the string
Echo $ {arrayz [@] # T * e} # One Two four five
#
The matching expression acts on all elements of the array.
#
Match "three" and delete it.
# Minimum matching of string tail
Echo $ {arrayz [@] % H * e} # One Two t four five
#
The matching expression acts on all elements of the array.
#
Match "hree" and delete it.
# Longest matching at the end of a string
Echo $ {arrayz [@] % T * e} # One Two four five
#
The matching expression acts on all elements of the array.
#
Match "three" and delete it.
# Substring replacement
# The first matched substring will be replaced
Echo $ {arrayz [@]/fiv/xyz} # One two three four xyze
#
The matching expression acts on all elements of the array.
# All matched substrings will be replaced
Echo $ {arrayz [@] // IV/yy} # One two three four fyye
#
The matching expression acts on all elements of the array.
# Delete all matched substrings
# Replacing strings without being specified means deleting
Echo $ {arrayz [@] // fi/} # One two three four ve
#
The matching expression acts on all elements of the array.
# Replace the front-end string
Echo $ {arrayz [@]/# fi/XY} # One two three four xyve
#
The matching expression acts on all elements of the array.
# Replace the last string
Echo $ {arrayz [@]/% VE/ZZ} # One two three four fizz
#
The matching expression acts on all elements of the array.