Use array in Shell
A shell variable is able to hold a single value. This type of variables is called scalar variables.
The shell supports different types of variables called an array variable that can hold multiple values at the same time. Arrays provide a method for grouping a set of variables. Instead of creating a new name for each required variable, you can use an array variable to store all other variables.
All name rules are discussed in chapter Shell variables that will be applied during array naming.
Define array values in Unix / Linux
The difference between an array variable and a scalar variable can be explained as follows:
When you are trying to represent the names of different students as a set of variables. Each single variable is a scalar variable as follows:
NAME01 = "Zara" NAME02 = "Qadir" NAME03 = "Mahnaz" NAME04 = "Ayan" NAME05 = "Daisy"
We use a single array to keep all the names mentioned above. The following is a simple method for creating an array variable to assign a value to one of them. This is described as follows:
array_name [ index ] = value
Here array_name is the name of the array, index is the index of the items in the array you want to set and value is the value you want to set for that item.
Here is an example, the following commands:
NAME [ 0 ] = "Zara" NAME [ 1 ] = "Qadir" NAME [ 2 ] = "Mahnaz" NAME [ 3 ] = "Ayan" NAME [ 4 ] = "Daisy"
If you are using ksh shell, then the syntax of the array is:
set - A array_name value1 value2 . valuen
If you are using a bash shell, here the array syntax is:
array_name = ( value1 . valuen )
Access to array values in Unix / Linux
After you have set any array value, you can access it as follows:
Here array_name is the array name, and index is the index of the values to be accessed. Here is a simple example:
#! / bin / sh NAME [ 0 ] = "Zara" NAME [ 1 ] = "Qadir" NAME [ 2 ] = "Mahnaz" NAME [ 3 ] = "Ayan" NAME [ 4 ] = "Daisy" echo "First Index : " echo " Second Index: "
It will produce the following result:
$ ./ test . sh First Index : Zara Second Index : Qadir
You can access all items in an array in one of the following ways:
$ { array_name [*]} $ { array_name [@]}
Here array_name is the name of the array you are interested in. Here is a simple example:
#! / bin / sh NAME [ 0 ] = "Zara" NAME [ 1 ] = "Qadir" NAME [ 2 ] = "Mahnaz" NAME [ 3 ] = "Ayan" NAME [ 4 ] = "Daisy" echo "First Method : $ {NAME [*]} "
It will produce the following result:
$ ./ test . sh First Method : Zara Qadir Mahnaz Ayan Daisy Second Method : Zara Qadir Mahnaz Ayan Daisy
According to Tutorialspoint
Previous article: Special variables in Unix / Linux
Next lesson: Basic Shell operators
You should read it
May be interested
- How to copy one array into another array in Golangin go, an array is a fixed-length sequence containing elements of a specific type. copying one array to another is simple but requires both arrays to be of the same length and type.
- How to create and run shell scripts in Ubuntu 22.04shell is a user-written command interpreter. script shell helps users write and execute multiple commands at the same time. in this article, readers will learn how to execute shell scripts through command line input.
- What is Linux Shell? The most popular Linux Shellsare you satisfied using the bash shell in linux? or do you want to try an alternative? there are tsch, fish, kornshell and z shell for you to choose from. but which popular linux shell is best?
- Array formulas in Excel - Tutorials and examplesarray formulas in excel - tutorials and examples. an array formula is a formula that can perform multiple calculations of one or more items in an array. array formulas can return multiple values or a single result.
- Nilesoft Shell - Menu customization application in Windows Explorerif you want more control, try nilesoft shell, an open source tool that lets you personalize the context menu to your liking.
- Shell Sort in data structure and algorithmshell sort is a highly efficient sorting algorithm based on insertion sorting algorithm (insertion sort). this algorithm avoids the case of swapping positions of two distant elements in the selection algorithm (if the smaller element is in the right position quite far from the larger element on the left).
- Learn about Collection of Record in JavaScriptin the previous article, we introduced you to a couple of 2-dimensional array features - 2d array in javascript, in many cases applied when we need to keep information about 1 or more lists of numbers of strings certain data, array object will be the most commonly used and most used tool ...
- Array (Array) in PHPan array is a data structure that stores one or more of the same value types in a single value.
- Instructions and examples of array formulas Array Formulas in Excelthe following article details array formulas and practical examples in excel to help you better understand and apply them in your work most effectively.
- Top 10 best array games for PCtop 10 best array games for pc you prefer to direct and command soldiers to occupy new bases and lands. simulate epic battles in history through strategy games, arrayed games. let's tipsmake.com find the top