UNIX / Linux Using Shell Arrays - Unix/Linux

What are UNIX / Linux Using Shell Arrays?

In this section, we will consider how to use shell arrays in UNIX. A shell variable is capable enough to hold a single value. These variables are called scalar variables.

Shell supports a different kind of variable called an array variable. This can hold several values at the same time. Arrays supply a scheme of grouping a set of variables. In its place of creating a new name for each variable that is essential, you can use a single array variable that stores all the other variables.

All the naming rules discuss for Shell Variables would be relevant while naming arrays.

Defining Array Values

The differentiation between an array variable and a scalar variable can be explained as follows.

Understand you are trying to represent the names of different students as a set of variables. Each of the individual variables is a scalar variable as follows –

We can employ a single array to store all the above mention names. Following is the simplest process of creating an array variable. This helps allocate a value to one of its indices.

Here array name is the name of the array, index is the index of the item in the array that you desire to set, and value is the value you want to set for that item.

As an instance, the following commands –

If you are using the kasha shell, here is the syntax of array initialization –

If you are using the bash shell, here is the syntax of array initialization –

Accessing Array Values

Following you have set any array variable, you access it as follows –

Here array name is the name of the array, and index is the index of the value to be access. Following is an instance to realize the concept –

The above instance will create the following result –

You can access all the items in an array in one of the following conduct –

Here array name is the name of the array you are attracted in. Following illustration will help you recognize the model –

The above instance will make the following result –

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Unix/Linux Topics