• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 732
  • Last Modified:

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

0
mahome
Asked:
mahome
  • 5
  • 3
  • 2
2 Solutions
 
woolmilkporcCommented:

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
 
Pétur Ingi EgilssonSoftware Engineer -- ConsultantCommented:
#!/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
 
Pétur Ingi EgilssonSoftware Engineer -- ConsultantCommented:
Example output for the above post:

petur@nat:~$ ./script.sh
arr[0] = 123
arr[1] = 345
arr[2] = 468
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
mahomeAuthor Commented:
@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
 
woolmilkporcCommented:
Unfortunately, no idea. Researched a bit, found nothing (at all!)
Good luck anyway,
cheers
wmp
 
0
 
mahomeAuthor Commented:
I've got it: the filter for SET:

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

Open in new window

0
 
woolmilkporcCommented:
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
 
woolmilkporcCommented:
... 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
 
mahomeAuthor Commented:
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
 
woolmilkporcCommented:
OK,
I reproduced that with bash. Tricky, indeed. Good solution!
wmp
 
 
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now