LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-09-2018, 01:37 PM   #1
Dantaliion
LQ Newbie
 
Registered: Nov 2017
Posts: 18

Rep: Reputation: Disabled
Using a variable's value inside the name of another variable


Hi. I have a function which collects some information about multiple people, and, to shorten my code, i am trying to get all i need with one funtion...
Snippet:

Code:
person_info() {
  person="$1"
  "$person"_firstname=$(read -p "Firstname: " fn; echo $fn)
  "$person"_nickname=$(read -p "Nickname: " nn; echo $nn)
  ...
You probably already figured out what i am trying to do.
I tryed looking this up, but nothing came up about actually NAMING the variable with the value of another variable..
 
Old 01-09-2018, 01:58 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro
Posts: 6,399

Rep: Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238Reputation: 1238
Code:
userx@solus ~ $ var1="var1"
userx@solus ~ $ var2="var2"
userx@solus ~ $ var3="$var1 "" $var2"
userx@solus ~ $ echo $var3
var1 var2
not really sure what you're talking about. naming a "new" var with the value inside of another var? dynamically creating variables using the value within a different var?

Last edited by BW-userx; 01-09-2018 at 02:00 PM.
 
1 members found this post helpful.
Old 01-09-2018, 02:03 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,667
Blog Entries: 6

Rep: Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531
The first part of what you are looking for is indirection, look for indirect expansion in the bash manual (you do not say what shell you are using). Here is part of what you will find there:

Code:
If  the  first  character  of  parameter is an exclamation point (!), a level of variable indirection is
introduced.  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.
Using indirect expansion to set the value of the indirect variable is most easily done using an eval'd statement. You can be creative with this, and it can get messy, but here is an example which illustrates the basics with two levels of indirection:

Code:
varname="THEVAR"
eval "${varname}=ANOTHERVAR"
eval "${!varname}=27"
echo "${varname} = ${!varname}"
eval "echo ${!varname} = \$${!varname}"

...which produces...

THEVAR = ANOTHERVAR
ANOTHERVAR = 27
Your description, collecting multiple fields on multiple people would also suggest that an array based method might be preferable.

Last edited by astrogeek; 01-09-2018 at 02:18 PM. Reason: Better example
 
1 members found this post helpful.
Old 01-09-2018, 07:31 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,608

Rep: Reputation: 2936Reputation: 2936Reputation: 2936Reputation: 2936Reputation: 2936Reputation: 2936Reputation: 2936Reputation: 2936Reputation: 2936Reputation: 2936Reputation: 2936
The answer about indirection has been provided, but the solution I would use in this case would be an associative array (assuming bash 4+) then you could use the array elsewhere with multiple data in it:
Code:
personinfo()
{
  declare -A firstname nickname

  person=$1

  read -r -p "Enter Firstname: " firstname[$person]
  read -r -p "Enter Nickname:  " nickname[$person]
}
If array only needed in that function then change declare to local
 
1 members found this post helpful.
Old 01-11-2018, 12:44 PM   #5
Dantaliion
LQ Newbie
 
Registered: Nov 2017
Posts: 18

Original Poster
Rep: Reputation: Disabled
I ended up using a solidification function for each person as in:
Code:
anna_solidify() {
  anna_firstname = "$firstname"
  ...
  ...
}
And so on...
I marked your answers as helpful because i learned stuff i didn't know before and will definetely help in the future... Thanks!
 
Old 01-11-2018, 01:07 PM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166
As "impressive" as Bash's own built-in scripting language might be, or might become, I suggest that instead you use a real scripting language – PHP, Perl, Ruby, Python, etc. – using bash's #shebang feature to invoke it.

Once there, don't use variables to coin-up the names of other variables. Instead, use associative arrays – what some languages call "hashes." You are managing a collection of values, and these languages have explicit support for that idea without the possibility of corrupting the variable-name namespace. Also, since these languages naturally embody these concepts and are meant to do so, the resulting code is much more clear.

Bash really doesn't care which language you use to write a "bash script."
 
1 members found this post helpful.
Old 01-11-2018, 07:29 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,354

Rep: Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509
Quote:
Originally Posted by sundialsvcs View Post
using bash's #shebang feature to invoke it.
[...]
Bash really doesn't care which language you use to write a "bash script."
Minor point of order: it's a Linux feature, not a bash feature.

See execve(2).
 
Old 01-12-2018, 08:24 AM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166
Quote:
Originally Posted by ntubski View Post
Minor point of order: it's a Linux feature, not a bash feature.

See execve(2).
Thank you for the clarification. (nit, nit nit ... )
 
Old 01-12-2018, 08:30 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,504

Rep: Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080
Well, it is a Unix feature, not a Linux feature.
 
Old 01-12-2018, 08:44 AM   #10
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,707

Rep: Reputation: 1035Reputation: 1035Reputation: 1035Reputation: 1035Reputation: 1035Reputation: 1035Reputation: 1035Reputation: 1035
Dunno if this is any help https://centos.tips/nesting-variables-in-bash/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Python (Jinja2) - Use variable inside a variable vikas027 Programming 1 10-07-2015 12:13 AM
Calculation using variable inside variable unclesamcrazy Linux - Newbie 7 04-21-2015 09:24 AM
[bash] re-evaluate variable inside variable muzzol Programming 9 12-01-2010 11:31 AM
can we use variable inside another variable amemyum Linux - Newbie 13 11-16-2009 03:44 PM
Retrieving from a variable whose name is inside a variable. thekillerbean Linux - General 4 02-09-2006 08:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration