How to create dynamic variable names in Bash?

How to create dynamic variable names in Bash?

If you can’t use associative arrays (e.g., you must support bash 3), you can use declare to create dynamic variable names: and use indirect parameter expansion to access the value. See BashFAQ: Indirection – Evaluating indirect/reference variables.

How can I generate new variable names in a shell?

Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion.” You’re using the value of i as if it were an array index.

Is it possible to generate variable names in the fly?

I’m trying to generate dynamic var names in a shell script to process a set of files with distinct names in a loop as follows: Is it possible generate var names in the fly? “If the first character of parameter is an exclamation point (!), a level of variable indirection is introduced.

How to store the name of a variable in Bash?

You can simply store the name of the variable in an indirection variable, not unlike a C pointer. Bash then has a syntax for reading the aliased variable: $ {!name} expands to the value of the variable whose name is the value of the variable name. You can think of it as a two-stage expansion: $ {!name} expands to $var_37, which expands to lolilol.

Why are all variables expanded in Bash commandline?

This is correct, but maybe in a different way than you may have thought. The shell parses the commandline in several distinct steps and one of these steps is the “expansion” of variables – that is, replacing them with their values. The problem is that all variables are expanded at the same time.

Why do I need to eval a variable in Bash?

The problem is that all variables are expanded at the same time. Therefore you need eval, because your construct implies a “second pass of expansion” to be applied onto your line. Unfortunately you cannot selectively restart a certain parsing step. You can only restart the whole process from the beginning.

How to count the number of command variations in Bash?

Notice that the shell “consumes” some characters and if you need to have them still there you need to escape them properly so that they are not consumed in the first pass: To really understand all the differences between these lines pipe the echo-output into wc -w to count the words for every command variation.

How to assign a variable to another variable in Bash?

I was curious to know how to assign to a variable with another variable in its name. How about using another variable to hold the dynamic name and use it for retrieving the value after setting? Unfortunately, Bash doesn’t allow declare $new_var=”123″ – that would have made this a little prettier.

How to get the name of the aliased variable in Bash?

Just as in Method 1, the reference stores the name of the aliased variable, but each time the reference is accessed (either for reading or assigning), Bash automatically resolves the indirection. In addition, Bash has a special and very confusing syntax for getting the value of the reference itself, judge by yourself: $ {!ref}.

How is the name of a variable expanded in Bash?

Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself.

How to declare the name of a variable?

(The caveat being that we’re assuming name_of_variable contains nothing but a valid variable name, and one we are free to use: not something special.) You can use declare and !, like this: