Paolo
Paolo

Reputation: 2249

A variable of which a part is variable

I have variables that look something like this:

$INFOMonday
$INFOTuesday

In my Bash script, I would like to use the value of todays variable, so to say. I extracted the day with:

dayofweek=$(date --date=${dateinfile#?_} "+%A")

What I need help with, is doing the following:

echo $INFO$dayofweek

Is there any way of adding a variable as part of a variables name? For example, if it's monday, the echo would return the value of $INFOMonday.

Upvotes: 2

Views: 107

Answers (1)

rici
rici

Reputation: 241701

The old-style way of doing this is with the indirection operator ${!variable}:

dayofweek=$(date...)
var=INFO$dayofweek
echo ${!var}

Note that in ${!var}, var must be a variable-name, not a string expression or other type of substitution. It's a little clunky.

The newer way of doing it would be with an associative array.

Upvotes: 2

Related Questions