Create variable from string/nameonly parameter to extract data in bash?

前端 未结 3 746
别那么骄傲
别那么骄傲 2020-12-12 03:31

I want to save the variable name and its contents easily from my script.

Currently :-

LOGFILE=/root/log.txt
TEST=/file/path
echo \"TEST : ${TEST}\" &         


        
3条回答
  •  [愿得一人]
    2020-12-12 04:32

    Yes, using indirect expansion:

    echo "$1 : ${!1}"
    

    Quoting from Bash reference manual:

    The basic form of parameter expansion is ${parameter} [...] 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

提交回复
热议问题