Linux shell arrays

Source: Internet
Author: User

From:http://www.jb51.net/article/34322.htm

The bash shell supports only one-dimensional arrays, but there is no limit to the number of arguments.

Declares an array:
Declare-a Array
(In fact, you don't have to declare it, you assign it directly to the variable as an array, and bash knows it's an array)

Array Assignment:
(1) array= (var1 var2 var3 ... varn)
(2) array= ([0]=VAR1 [1]=var2] [2]=var3 ... [N]=varn]
(3) Array[0]=var1
Arrya[1]=var2
...
Array[n]=varn

To calculate the number of array elements:
${#array [@]} or ${#array [*]}

Bash's special parameters @ and * Both denote "extended position parameters, starting from 1", but the form is slightly different, but it seems to be common in arrays.

Reference array:

Copy CodeThe code is as follows:
Echo ${array[n]}

To iterate over an array:

Copy CodeThe code is as follows:
Filename= (' ls ')
For Var in ${filename[@]};d o
Echo $var
Done

Here are a few examples of shell arrays.

1. Read n string from "standard input", and each input string is saved in array

Copy CodeThe code is as follows:
#!/bin/bash
I=0
N=5
While ["$i"-lt $n]; Do
echo "Please input strings ... ' expr $i + 1 '"
Read array[$i]
b=${array[$i]}
echo "$b"
i= ' expr $i + 1 '
Done

2. Put the letters in the string into the array, and output to the "standard output"

Copy CodeThe code is as follows:
#!/bin/bash
chars= ' ABCDEFGHIJKLMNOPQRSTUVWXYZ '
for ((i=0; i<26; i++)); Do
array[$i]=${chars: $i: 1}
echo ${array[$i]}
Done

The interesting place here is ${chars: $i: 1}, which means to get 1 characters starting from the $i position of the chars string. If you change 1 to 3, you get 3 characters ~ The result is:
Abc
Bcd
...
Vxy
Xyz
YZ//There is not enough string to get the
z//There is not enough string to get the

Here are some examples of applying arrays to shell environment variables.

3. Apply the array to the shell environment variable (1)

Copy CodeThe code is as follows:
Array Assignment:
[[email protected] ~]# season= ("srping" "Summer" "Autumn" "Winter")
When you find that the assignment is wrong, you can immediately correct it from the new assignment, as the above Spring is written as srping.
Re-assignment: (The original value is overridden)
[[email protected] ~]# season= ("Spring" "Summer" "Autumn" "Winter")

Look at the environment variables:
[[Email protected] ~]# Set | grep SEASON
season= ([0]= "Spring" [1]= "Summer" [2]= "Autumn" [3]= "Winter")

Show the entire array:
[[email protected] ~]# echo ${season[*]} or echo ${season[@]}
Spring Summer Autumn Winter

To display an array element:
[[email protected] ~]# echo ${season[3]}
Winter

Assigning a value to a single array element:
[Email protected] ~]# season[0]= "new_spring"

Look at the array again:
[[email protected] ~]# echo ${season[*]}
New_spring Summer Autumn Winter

Clears the specified single array element:
[Email protected] ~]# unset season[2]

Clear the entire array:
[Email protected] ~]# unset SEASON

4. Apply the array to the shell environment variable (2)
Use the TR command to convert a carriage return from a file into a space:

Copy CodeThe code is as follows:
[Email protected] ~]# Cat/etc/shells | TR "\ n" "" >/tmp/tmp.file


Assigns the contents of a file to an array: (the content before the first carriage return)

Copy CodeThe code is as follows:
[Email protected] ~]# read-a Shells </tmp/tmp.file


To view array assignment conditions:

Copy CodeThe code is as follows:
[[Email protected] ~]# Set | grep "Shells"
shells= ([0]= "/bin/sh" [1]= "/bin/bash" [2]= "/sbin/nologin" [3]= "/bin/tcsh" [4]= "/bin/csh" [5]= "/bin/ksh")


You can apply this array environment variable to other shell scripts or applications later.

Linux shell arrays

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.