How to list variables declared in script in bash?

后端 未结 14 923
走了就别回头了
走了就别回头了 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:26

    Try this : set | egrep "^\w+=" (with or without the | less piping)

    The first proposed solution, ( set -o posix ; set ) | less, works but has a drawback: it transmits control codes to the terminal, so they are not displayed properly. So for example, if there is (likely) a IFS=$' \t\n' variable, we can see:

    IFS='
    '
    

    …instead.

    My egrep solution displays this (and eventually other similars ones) properly.

    0 讨论(0)
  • 2020-11-28 20:27

    The printenv command:

    printenv prints all environment variables along with their values.

    Good Luck...

    0 讨论(0)
  • 2020-11-28 20:29

    Based on some of the above answers, this worked for me:

    before=$(set -o posix; set | sort);
    

    source file:

    comm -13 <(printf %s "$before") <(set -o posix; set | sort | uniq) 
    
    0 讨论(0)
  • 2020-11-28 20:32

    I probably have stolen the answer while ago ... anyway slightly different as a func:

        ##
        # usage source bin/nps-bash-util-funcs
        # doEchoVars
        doEchoVars(){
    
            # if the tmp dir does not exist
            test -z ${tmp_dir} && \
            export tmp_dir="$(cd "$(dirname $0)/../../.."; pwd)""/dat/log/.tmp.$$" && \
            mkdir -p "$tmp_dir" && \
            ( set -o posix ; set )| sort >"$tmp_dir/.vars.before"
    
    
            ( set -o posix ; set ) | sort >"$tmp_dir/.vars.after"
            cmd="$(comm -3 $tmp_dir/.vars.before $tmp_dir/.vars.after | perl -ne 's#\s+##g;print "\n $_ "' )"
            echo -e "$cmd"
        } 
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-11-28 20:33

    set will output the variables, unfortunately it will also output the functions defines as well.

    Luckily POSIX mode only outputs the variables:

    ( set -o posix ; set ) | less
    

    Piping to less, or redirect to where you want the options.

    So to get the variables declared in just the script:

    ( set -o posix ; set ) >/tmp/variables.before
    source script
    ( set -o posix ; set ) >/tmp/variables.after
    diff /tmp/variables.before /tmp/variables.after
    rm /tmp/variables.before /tmp/variables.after
    

    (Or at least something based on that :-) )

    0 讨论(0)
提交回复
热议问题