In this article we will show you a bash script code for creating
nameref for accessing a variable from another variable. This is very useful as it creates a dynamic linking between the variable name and value of another variable.
Code Example –
#!/bin/bash first_name="Akash" first_pet="jackie (pup)" field="name" declare -n ref=first_$field echo $ref # Output: # Akash
2. Naming variable based on another variable –
#!/bin/bash first_name="Akash" first_pet="jackie (pup)" field="love" declare "first_$field=mathematics" echo $first_love # Output: # mathematics
Inspired from Dylan Araps