Get the App
SLTechnology News&Howtos  ›  Servers  › 

Linux shell array assignment method

Shulou Source: shulou.com Published: 2022-06-02 06:01:19 10月02日 Update

In Bash, there are two ways to assign array variables:

(1) name = value1... Valuen) the subscript starts at 0 at this time

(2) name [index] = value

The following is illustrated by a simple script, which is as follows:

#! / bin/bash

# define an array

A = (a b c def)

# display the array as a string output

Echo ${A [@]}

# screen display: a b c def

# the length of the array represents ${# A [*]}

Len=$ {# A [*]}

Echo ${# A [*]}

# screen display: 4

# change the value of array elements

A [3] = 'vivian'

Echo ${A [*]}

# screen display: a b c vivian

# Loop output array elements

ITunes 0

While [$I-lt $len]

Do

Echo ${A [$I]}

Let iTunes +

Done

# screen output:

# a

# b

# c

# vivian

# another way to write the output array elements of the loop, note that in the condition, the reference variable must be in double quotes, otherwise an error will be reported

For ((jacu0; j)

Tags: Array element output screen loop writing variable script method subscript content character string quotation mark condition length Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Redmi Shulou Information Apple Shulou Tech Info