Register
It is currently Mon Apr 23, 2018 6:53 pm

writing a procedure, which echo variable's name -> how to?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Fri Mar 09, 2018 11:36 am   

Joined: Fri Mar 09, 2018 11:10 am
Posts: 1
Hello,

I'd like to write a procedure, which receives a variable name as a parameter and print/echo it's value.

I thought on something like following:
Code:
procedure ec () {
  echo "Variable $1 has a value $1"
}

But the above code doesn't work...

So, how can I write such procedure?

Thank you!


Top
 Profile  
 PostPosted: Fri Mar 09, 2018 11:25 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 574
use indirect expansion. See
Code:
man bash | sed -rn '/Parameter Expansion/,/The exclam/p'

Try this:
Code:
my_var="something"
indirect_addressing_of_an_var(){
    echo ${!1}
}
indirect_addressing_of_an_var my_var


Top
 Profile  
 PostPosted: Wed Mar 14, 2018 3:24 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 642
hello,

Code:
nameRefUsage()
{
   declare -n inVar="$1"
   echo "$1 = $inVar"
}
var="abc def"
nameRefUsage var


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 23 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP