Link to home
Start Free TrialLog in
Avatar of mahome
mahomeFlag for Germany

asked on

How to display all defined variables with values?

Is there a command which displays all defined variables in this context with value, like env does for environment variables.
script
------
A=123
B=456
C=$A$B
 
 
desired result (like env)
-----------------------
A=123
B=456
C=123456

Open in new window

SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
#!/bin/bash
#Pétur Ingi Egilsson

arr[0]=123;
arr[1]=345;
arr[2]=$((${arr[0]}+${arr[1]}));
#arr[3]=yourvalue
#arr[4]=yourvalue etc...

for i in `seq 0 $((${#arr[@]}-1))`
do
        echo arr[$i] = ${arr[$i]};
done
Example output for the above post:

petur@nat:~$ ./script.sh
arr[0] = 123
arr[1] = 345
arr[2] = 468
Avatar of mahome

ASKER

@woolmilkporc
I've just had a look at SET. There is all included I'm looking for. But I have no possibility for prefixing my variables. It would be adequate if would find a way to seperate the variables from the functions of the output of SET. Any idea?



@PeturIngiEgilsson
Please look again at my question, that's not what I'm looking at. I want a list of all defined variables, like env does for environment variables.



Unfortunately, no idea. Researched a bit, found nothing (at all!)
Good luck anyway,
cheers
wmp
 
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Could you please explain? I see no real difference to 'set' alone.
... and why 'next' and 'exit' ? Leave them both out, and nothing changes, as far as I can see.
wmp
 
... or is it to leave out the lines without '=' ?
If yes, a 'set | grep "="  would do.
It didn't see this difference immediately, because I use ksh here, whose 'set' doesn't display functions. Now I understand your posting "... separate variables from functions ..."
wmp
 
Avatar of mahome

ASKER

Hmm than you have a different output. I'm using ubuntu and bash.
In my output there are also functions defined, see snippet. And I just want the variables. With a simple grep I would also find the = in the functions which are not defined variables. Therefore I had to search for the first line without a =.

XAUTHORITY=/home/martin/.Xauthority
XDG_DATA_DIRS=/usr/local/share/:/usr/share/:/usr/share/gdm/
XDG_SESSION_COOKIE=7e618c28d92ffa170fbd73004804ae47-1240899866.46765-656823831
_=set
bash205='3.2.39(1)-release'
bash205b='3.2.39(1)-release'
bash3='3.2.39(1)-release'
_ImageMagick () 
{ 
    local prev;
    prev=${COMP_WORDS[COMP_CWORD-1]};
    case "$prev" in 
        -channel)
            COMPREPLY=($( compgen -W 'Red Green Blue Opacity \
                                Matte Cyan Magenta Yellow Black' -- $cur ));
            return 0
        ;;
        -colormap)
            COMPREPLY=($( compgen -W 'shared private' -- $cur ));
            return 0
        ;;
        -colorspace)
            COMPREPLY=($( compgen -W 'GRAY OHTA RGB Transparent \
                                XYZ YCbCr YIQ YPbPr YUV CMYK' -- $cur ));
            return 0
        ;;
        -compose)
            COMPREPLY=($( compgen -W 'Over In Out Atop Xor Plus \
:

Open in new window

OK,
I reproduced that with bash. Tricky, indeed. Good solution!
wmp