Get the App
SLTechnology News&Howtos  ›  Development  › 

How to manipulate shell arrays

Shulou Source: shulou.com Published: 2022-06-03 08:13:20 09月22日 Update

This article focuses on "how to operate the shell array", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to manipulate shell arrays.

1. Array declaration:

The code is as follows:

1) array [key] = value # array [0] = one,array [1] = two

2) declare-an array # array is used as the array name

3) array= (value1 value2 value3...)

4) array= ([1] = one [2] = two [3] = three.)

5) array= "one two three" # echo ${array [0 | @ | *]}, treat the array variable as an array, but the array element is only the string itself

two。 Array access:

The code is as follows:

1) ${array [key]} # ${array [1]}

3. Deletion of array

The code is as follows:

1) unset array [1] # Delete the first element in the array

2) unset array # delete the entire array

4. Calculate the length of the array:

The code is as follows:

1) ${# array}

2) ${# array [0]} # Ibid. ${# array [*]}, ${# array [@]} Note the difference with # {array:0}

5. Extraction of array

Extract from the tail:

The code is as follows:

Array= ([0] = one [1] = two [2] = three [3] = four)

${array [@]: 1} # two three four, all elements after the first element is removed, then ${array [@]: 0} represents all elements

${array [@]: 0:2} # one two

${array [@]: 1:2} # two three

6. Substring deletion

The code is as follows:

[root@localhost dev] # echo ${array [@]: 0}

One two three four

[root@localhost dev] # echo ${array [@] # tweee} # the shortest match begins on the left: "tweee", which will match to "thre"

One two e four

[root@localhost dev] # echo ${array [@] # # tweee} # the longest match begins on the left, which will match to "three"

[root@localhost dev] # array= ([0] = one [1] = two [2] = three [3] = four)

[root@localhost dev] # echo ${array [@]% o} # shortest match starting at the end of the string

One tw three four

[root@localhost dev] # echo ${array [@]% o} # longest match starting at the end of the string

One tw three four

7. Substring substitution

The code is as follows:

[root@localhost dev] # array= ([0] = one [1] = two [2] = three [3] = four) [code]

The first match will be deleted.

[code] [root@localhost dev] # echo ${array [@] / oago}

Mne twm three fmur

All matches will be deleted.

The code is as follows:

[root@localhost dev] # echo ${array [@] / / obox}

Mne twm three fmur

If no replacement substring is specified, the matching substring is deleted

The code is as follows:

[root@localhost dev] # echo ${array [@] / / o /}

Ne tw three fur

Replace the front terminal string of the string

The code is as follows:

[root@localhost dev] # echo ${array [@] / # oplink}

Kne two three four

Replace the string after the terminal string

The code is as follows:

[root@localhost dev] # echo ${array [@] /% oplink}

One twk three four

At this point, I believe you have a deeper understanding of "how to manipulate shell arrays". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Array code element character string longest content learning practical deeper interest front end variable only practical practical tail simple operation method more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Xiaomi Redmi OPPO Reno macOS