Indirect expansion returns variable name instead of value

SVill

I am trying to set up some variables using indirect expansion. According to the documentation I've read, the set up should be simple:

var1=qa
qa_num=12345
varname="${var1}_ci"

echo ${!varname}

I should be getting "12345". Instead, the output is "varname". If I remove the exclamation point, I end up with "qa_ci", not "12345"

This should be a relatively simple solution, so I'm not sure what I'm missing, if anything.

cxw

Your code defines qa_num, but the varname assignment references qa_ci. As a result, your echo was expanding nonexistent qa_ci, giving empty results. Changing the varname assignment fixes the problem on my system.

Example: foo.sh:

#!/bin/bash
var1=qa
qa_num=12345
varname="${var1}_num"     # <=== not _ci

echo "${!varname}"        # I also added "" here as a general good practice

Output:

$ bash foo.sh
12345

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Prolog returns variable name instead of value

Powershell indirect expansion variable

indirect variable expansion in POSIX as done in bash?

Understand two examples using indirect expansion for variable expansion in arithmetic expressions

Excel Indirect command returns #value

Sql: Query returns column name instead of Value

MySQL returns the name of the column instead of the value

Assigning a function to the value of a variable instead of the variable name

EPPlus returns #VALUE! instead of the cell content when used with Formula OFFSET, INDIRECT & MATCH

Bash get the length of indirect variable expansion with a list as the variable

display variable name instead of value in foreach loop

Python get variable name instead of value

AngularJS returns variable name in curley brackets, not the value

Indirect access column name with object variable

Lambda function returns function name instead of value in Python

PHP PDO mySQL query returns column name instead of value

GetDetailsOf returns property name instead of value (delphi 2007)

Adding variable name instead of value of variable to select list

Lua puts of variable name instead of variable value in table

How do i print Variable name instead of the value assigned to the variable?

Why is the object property using variable name as key instead of variable value?

Laravel query returns question mark instead of variable value

evalf in sympy returns expression instead of value in single variable expression

Using context variable to name a "section" instead of passing a value

Macro variable is not printing the table value but instead the column name

How can I get Matlab to use the variable value instead of name

Can't use $refs using variable name instead of direct value

create variable of select option name instead of value in php

Bash - How to show variable value instead of name in background process descriptions?