Pill: Use arrays with dynamic name in Bash

in #bash6 years ago

Bash-Final.jpg

One of the great advantages of interpreted languages is that we can store in a variable the name of another variable and work with its value. Pointers? Well, in the end it is similar to the use of pointers in C, although here we can refer to the same name of the variable even, previously filtered and very carefully, ask the user the variable to work with it.

This can give us a lot of flexibility in our code. For example, I have used it to perfect my configuration file reading script, which we can also pass the name of the variable to store the configuration.

Arrays with dynamic name

In Bash, accessing a variable whose name is contained within another variable is simple, we can do it by calling $ {! NOMBREDEVARIABLE}. We can see an example in the post referenced.

The problem is that when they are arrays it is not so immediate. The objective is that, having a variable that contains the name of the array, we can access your information in a simple way. And the problem here comes in the Bash version that we are using. In this case we must do it one way or another depending on whether we use a version lower than 4.3 (more or less came out in 2014) or if we have a later version.

Example for Bash <4.3

I'm sorry, I do not like to use eval, but of course, for these versions of the interpreter we need to create a string of characters with the call we're going to make and then evaluate that string of characters. This will give us the value we seek. For example:

1 MIARRAY = ("zero" "one" "two" "three")
2 REFERENCE = "MIARRAY"
3
4 # What we need is to access each of the MIARRAY elements through REFERENCE.
5
6 eval echo \ $ {$ REFERENCE [2]}

If you look, when building the chain. The first dollar symbol ($) we escape with the bar so that it is not interpreted. The one that does have to interpret is the second, so that $ REFERENCE becomes the name of the array.

We can do it with associative arrays also without problem, but it is somewhat complicated and we perform too many tasks to refer to a variable. In addition it is not very elegant or intuitive to have to escape a dollar and not another.

Example for Bash> = 4.3

For these versions, we have a modifier in the declare order (also called typeset) through which we can create a reference variable that will behave exactly like our array:

1 MIARRAY = ("zero" "one" "two" "three")
2 declare -n REFERENCE = "MIARRAY"
3
4 echo $ {REFERENCE [2]}

In addition, in this way, the name of the variable can be given in another variable, with which we can allow the user to specify the variable name. Yes, very carefully.

Source: https://poesiabinaria.net/2018/07/pildora-utilizar-arrays-nombre-dinamico-bash/.

Sort:  

Congratulations @jaidercarvajal! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes received

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:
SteemitBoard World Cup Contest - Croatia vs England


Participate in the SteemitBoard World Cup Contest!
Collect World Cup badges and win free SBD
Support the Gold Sponsors of the contest: @good-karma and @lukestokes


Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Coin Marketplace

STEEM 0.28
TRX 0.11
JST 0.031
BTC 69371.89
ETH 3768.85
USDT 1.00
SBD 3.71