0

Often in a terminal session I do stuff like this:

$ Source="/bin"

$ AllFiles=$(ls $Source/* | wc -l)

$ echo $AllFiles
159

It is tedious typing echo $Source followed by echo $AllFiles, etc. to display the variables I've defined in the current terminal session. If I use printenv my screen is filled with stuff I didn't define. If I use declare -p the same thing happens.

How can I display only the variables I've defined in this terminal session?

1 Answer 1

1

declare -p will show you ALL the variables defined outside of environmental variables which will include what you are looking for but also 121 other variables you are not looking for.

The secret is to capture the default variables when gnome-terminal opens and subtract that from the list of current variables to display only the new variables.

Using listvar

Instructions on creating the listvar function are below. To see how the listvar function works open a terminal and type:

$ listvar

$ hey="Hey World :/"

$ listvar
declare -- hey="Hey World :/"

$ Word=Something

$ Number=25

$ Ubuntu=16.04

$ listvar
declare -- Number="25"
declare -- Ubuntu="16.04"
declare -- Word="Something"
declare -- hey="Hey World :/"

Creating listvar

Add this to the end of your ~/.bashrc:

# Below must be at end of ~/.bashrc
# Display newly created variables
function listvar () {

    # Get current variable list
    mapfile -t CurrVars <<< "$(declare -p)"

    # Loop through each current variable
    for CurrVar in "${CurrVars[@]}" ; do

        # Skip over unwanted variables that are dynamic
        [[ "$CurrVar" == "declare -a BASH_"*    ]] && continue
        [[ "$CurrVar" == "declare -a FUNCNAME"* ]] && continue
        [[ "$CurrVar" == "declare -a PIPESTAT"* ]] && continue
        [[ "$CurrVar" == "declare -- _=\"\""*   ]] && continue
#        [[ "$CurrVar" == "declare -- sh="*      ]] && continue

        # Skip over variables used in our listvar function
        [[ "$CurrVar" == "declare -a SaveVar"*  ]] && continue
        [[ "$CurrVar" == "declare -- SaveVar"*  ]] && continue
        [[ "$CurrVar" == "declare -a CurrVar"*  ]] && continue
        [[ "$CurrVar" == "declare -- CurrVar"*  ]] && continue
        [[ "$CurrVar" == "declare -a OldVar"*   ]] && continue
        [[ "$CurrVar" == "declare -- OldVar"*   ]] && continue
        [[ "$CurrVar" == 'declare -- _="SaveV'* ]] && continue
        [[ "$CurrVar" == 'declare -- _="listv'* ]] && continue

        OldVar=false
        for SaveVar in "${SaveVars[@]}" ; do
            # Is current variable existing at time of save?
            [[ "$CurrVar" == "$SaveVar" ]]         && { OldVar=true ; break ; }
        done
        [[ $OldVar == false ]] && echo "$CurrVar"
    done

} # listvar


# Empty array to prevent doubling everytime ~/.bashrc is resourced
SaveVars=()
# Save variable list before user creates new ones in shell
mapfile -t SaveVars <<< "$(declare -p)"

Save your ~/.bashrc file and open a new terminal session to use listvar function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.