How to list variables declared in script in bash?

后端 未结 14 985
走了就别回头了
走了就别回头了 2020-11-28 20:03

In my script in bash, there are lot of variables, and I have to make something to save them to file. My question is how to list all variables declared in my script and get l

14条回答
  •  春和景丽
    2020-11-28 20:32

    Simple way to do this is to use bash strict mode by setting system environment variables before running your script and to use diff to only sort the ones of your script :

    # Add this line at the top of your script :
    set > /tmp/old_vars.log
    
    # Add this line at the end of your script :
    set > /tmp/new_vars.log
    
    # Alternatively you can remove unwanted variables with grep (e.g., passwords) :
    set | grep -v "PASSWORD1=\|PASSWORD2=\|PASSWORD3=" > /tmp/new_vars.log
    
    # Now you can compare to sort variables of your script :
    diff /tmp/old_vars.log /tmp/new_vars.log | grep "^>" > /tmp/script_vars.log
    

    You can now retrieve variables of your script in /tmp/script_vars.log. Or at least something based on that!

提交回复
热议问题