Shell ----- Array

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.