?
Solved

How to display all defined variables with values?

Posted on 2009-04-24
10
Medium Priority
?
730 Views
Last Modified: 2013-12-26
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

0
Comment
Question by:mahome
  • 5
  • 3
  • 2
10 Comments
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 800 total points
ID: 24225057

Hi,
afaik there isn't.

There is 'set', which you could use in some special manner -
- name all your variables with a common prefix, such as 'MY_[varname]', then use

MY_A=123
MY_B=456
MY_C=$MY_A$MY_B  

set | grep MY_  

_=MY_C
MY_A=123
MY_B=456
MY_C=123456
   
Sorry, no better idea.

wmp
0
 
LVL 10

Expert Comment

by:Pétur Ingi Egilsson
ID: 24227167
#!/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
0
 
LVL 10

Expert Comment

by:Pétur Ingi Egilsson
ID: 24227180
Example output for the above post:

petur@nat:~$ ./script.sh
arr[0] = 123
arr[1] = 345
arr[2] = 468
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 10

Author Comment

by:mahome
ID: 24239398
@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.



0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24249742
Unfortunately, no idea. Researched a bit, found nothing (at all!)
Good luck anyway,
cheers
wmp
 
0
 
LVL 10

Accepted Solution

by:
mahome earned 0 total points
ID: 24249861
I've got it: the filter for SET:

set | awk -- '/=/ { print $0; next } { exit }' 

Open in new window

0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24250017
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
 
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24250097
... 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
 
0
 
LVL 10

Author Comment

by:mahome
ID: 24250203
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

0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24250263
OK,
I reproduced that with bash. Tricky, indeed. Good solution!
wmp
 
 
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article will show you step-by-step instructions to build your own NTP CentOS server.  The network diagram shows the best practice to setup the NTP server farm for redundancy.  This article also serves as your NTP server documentation.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month15 days, 18 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question