Initiating Dynamic Variables (Variable Variables) in Bash Shell Script

Initiating dynamic variables (variable variables) in bash shell script

You may try using the eval construct in BASH:

key="BASE_PATH"
value="/path/to/project/root"
# Assign $value to variable named "BASE_PATH"
eval ${key}="${value}"

# Now you have the variable named BASE_PATH you want
# This will get you output "/path/to/project/root"
echo $BASE_PATH

Then, just use it in your loop.


EDIT: this read loop creates a sub-shell which will not allow you to use them outside of the loop. You may restructure the read loop so that the sub-shell is not created:

# get the PHP output to a variable
php_output=`php test.php`

# parse the variable in a loop without creating a sub-shell
IFS=":"
while read -r key val; do
eval ${key}="${val}"
done <<< "$php_output"

echo $BASE_PATH

Dynamically read and store values in variables in shell

You can grow the array dynamically at each step. Assume you start with the count, initialize the empty array and add elements one by one.

count=4; fruits=(); 
for i in `seq "$count"`;
do read f; fruits+=( "$f" );
done;
echo "${fruits[@]}"

works in version:
Version AJM 93t+ 2010-06-21.

This works with BASH in AIX

Using variables from shell script for gnuplot

Thank @markp-fuso, it works !

I just had to replace "($1/${Utau_fr[${i}]})" by "(column(1)/${Utau_fr[${i}]})" because the dollar signs are interpreted as starting a shell variable.

Creating bash variables from a files in a folder

It's not a "quote around the text" issue, it's the variable declaration varr$c that's not working. You should be using declare instead.

This script solves your problem :

#/bin/bash

c=0
for file in *; do
declare varr$c=$file;
c=$((c+1));
done

Bash in Make: how to expand dynamic variable names?

Following the idea in my comment to Jonathan's answer, I found a way that involves starting another make process from within the fragment which executes the inner for loop:

.PHONY: list
list:
for car in ${CARS} ; do \
car_colours_var=$${car}_COLOURS ; \
${MAKE} CAR_COLOURS_VAR=$${car_colours_var} single_car ; \
done

.PHONY: single_car
single_car:
for colour in ${${CAR_COLOURS_VAR}} ; do \
echo $${colour} ; \
done

The key is the intermediary CAR_COLOURS_VAR which is passed to the child make process as a parameter, which can subsequently be expanded by that process via the ${${CAR_COLOURS_VAR}} syntax.

Creating a string variable name from the value of another string

I know that nobody will mention it, so here I go. You can use printf!

#!/bin/bash

CONFIG_OPTION="VENDOR_NAME"
CONFIG_VALUE="Default_Vendor"

printf -v "$CONFIG_OPTION" "%s" "$CONFIG_VALUE"

# Don't believe me?
echo "$VENDOR_NAME"

Defining a variable in shell script portion of Jenkins Pipeline

As @jxramos stated, Jenkins is trying to resolve the variables in the script. It interprets any $string as a variable that needs substitution.
The solution is to escape the $ of the in-script variables, as follows:

pipeline { 
agent any
stages {
stage('test stage'){
steps {
sh """#!/bin/bash
myvar=somevalue
echo "The value is \$myvar"
"""
}
}
}
}


Related Topics



Leave a reply



Submit