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

前端 未结 3 748
别那么骄傲
别那么骄傲 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:33

    You want to use Variable Indirection. Also, don't use the function keyword, it is not POSIX and also not necessary as long as you have () at the end of your function name.

    LOGFILE=/root/log.txt
    
    save()
    {
        echo "$1 : ${!1}" >> ${LOGFILE}
    }
    
    TEST=/file/path
    
    save TEST
    

    Proof of Concept

    $ TEST=foo; save(){ echo "$1 : ${!1}"; }; save TEST
    TEST : foo
    

提交回复
热议问题