How to display variable name itself rather than the value in shell

Feng Tian

For example:

a=s1;b=s2; for i in $a $b ; do echo $i ; done

I want to get a and b NOT s1 and s2

How could I do it?

chepner

If you are using bash or ksh, indirect parameter expansion is safer than using eval.

a=s1
b=s2

for i in a b; do
   echo "$i=${!i}"
done

If you are using zsh, the syntax is slightly different.

for i in a b; do
   echo "$i=${(P)i}"
done

Other shells might have different syntax; check its documentation for details.

If you are using a shell (e.g. /bin/sh or dash) without indirect parameter expansion, you'll have to use eval:

for i in a b; do
    eval "echo $i=\$$i"
done

Here, eval is safe because you had explicit control over the value of i. In general, any parameters expanded in the argument to eval should be validated to ensure you don't execute code you didn't expect to.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Reflectively look at a variable name, rather than value

From Dev

Editor Template LabelFor - display value rather than name

From Dev

Editor Template LabelFor - display value rather than name

From Dev

Passing a variable value, rather than its name, as a class

From Dev

Is it possible to print the actual name of a variable, rather than the value it contains?

From Dev

return the numeric value of a indexed variable rather than its name

From Dev

Passing a variable value, rather than its name, as a class

From Dev

in Perl, how to redirect a "print" to a variable rather than display on the screen

From Dev

How to assign a function, returned by another function, to a function variable? The result rather than the generating function itself

From Dev

Retrieve choice name rather than value

From Dev

Get value from IO rather than the computation itself

From Dev

Returning the truthiness of a variable rather than its value?

From Dev

Evaluate value of observable as a variable rather than a string

From Dev

How would I display the department_name rather than the department_id?

From Dev

How can I set Thunderbird's "Recipient" column to display my email address rather than a friendly name?

From Dev

Display the full country name rather than country code in WooCommerce?

From Dev

INSERT INTO sql query is using variable string rather than field name

From Dev

Rename by list() variable name rather than index in R

From Dev

Display value (result of formula) rather than formula in fomula bar

From Dev

How to make the name of a variable in java variable itself

From Dev

Use value of variable rather than keyword in python numpy.savez

From Dev

Why is my js function returning itself rather than the returned value? [Angular]

From Dev

easily getting the value ("active") a database entry represents rather than the database entry ("2") itself

From Dev

Why is my js function returning itself rather than the returned value? [Angular]

From Dev

How to Display VS Code output window to side rather than below

From Dev

how to display characters in jPasswordField rather than * sign in java?

From Dev

How to display an actual image in ListView rather than its location?

From Dev

How to rename a specific colname by name in a Matrix, rather than numerical index?

From Dev

Ruby: How to pass the object name to an array rather than the ID

Related Related

  1. 1

    Reflectively look at a variable name, rather than value

  2. 2

    Editor Template LabelFor - display value rather than name

  3. 3

    Editor Template LabelFor - display value rather than name

  4. 4

    Passing a variable value, rather than its name, as a class

  5. 5

    Is it possible to print the actual name of a variable, rather than the value it contains?

  6. 6

    return the numeric value of a indexed variable rather than its name

  7. 7

    Passing a variable value, rather than its name, as a class

  8. 8

    in Perl, how to redirect a "print" to a variable rather than display on the screen

  9. 9

    How to assign a function, returned by another function, to a function variable? The result rather than the generating function itself

  10. 10

    Retrieve choice name rather than value

  11. 11

    Get value from IO rather than the computation itself

  12. 12

    Returning the truthiness of a variable rather than its value?

  13. 13

    Evaluate value of observable as a variable rather than a string

  14. 14

    How would I display the department_name rather than the department_id?

  15. 15

    How can I set Thunderbird's "Recipient" column to display my email address rather than a friendly name?

  16. 16

    Display the full country name rather than country code in WooCommerce?

  17. 17

    INSERT INTO sql query is using variable string rather than field name

  18. 18

    Rename by list() variable name rather than index in R

  19. 19

    Display value (result of formula) rather than formula in fomula bar

  20. 20

    How to make the name of a variable in java variable itself

  21. 21

    Use value of variable rather than keyword in python numpy.savez

  22. 22

    Why is my js function returning itself rather than the returned value? [Angular]

  23. 23

    easily getting the value ("active") a database entry represents rather than the database entry ("2") itself

  24. 24

    Why is my js function returning itself rather than the returned value? [Angular]

  25. 25

    How to Display VS Code output window to side rather than below

  26. 26

    how to display characters in jPasswordField rather than * sign in java?

  27. 27

    How to display an actual image in ListView rather than its location?

  28. 28

    How to rename a specific colname by name in a Matrix, rather than numerical index?

  29. 29

    Ruby: How to pass the object name to an array rather than the ID

HotTag

Archive