Find the answer to your Linux question:
Results 1 to 4 of 4
i want to access the variable name's variable value as a=testing b=a how to print by only using variable b. echo ${$b} --> it should print testing, how to do ...
  1. #1
    Just Joined! sathiya's Avatar
    Join Date
    Feb 2008
    Location
    Bangalore, India
    Posts
    97

    accessing variable name's variable value

    i want to access the variable name's variable value as

    a=testing
    b=a

    how to print by only using variable b.
    echo ${$b} --> it should print testing, how to do that..
    ( i know this echo will say error )

    by the above example, if you are not able to understand.
    I have the b variable has value "a", which is the variable name "a",
    i need to access that variable, by knowing only the second variable name ????

  2. #2
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Code:
    % a=testing
    % b=a
    % eval print \$$b
    testing
    Use printf if your shell does not have the print builtin.

  3. #3
    Just Joined!
    Join Date
    Nov 2008
    Location
    China
    Posts
    4
    if using BASH, it can be

    Code:
    a=testing 
    b=a
    eval echo $$b

  4. #4
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Quote Originally Posted by fox000002 View Post
    if using BASH, it can be

    Code:
    a=testing 
    b=a
    eval echo $$b
    I believe you meant:
    Code:
    eval echo \$$b
    With recent bash versions it could be even:

    Code:
    $ a=testing 
    $ b=a
    $ printf "%s\n" "${!b}"
    testing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...