Show All Set Variables


Recommended Posts

Is ther a way to show(list) all set and/or exported varibles in a bash shell?

edit added later//

I have a substitution question. Lets say I do this

shane@mainbox shane $ DATE=${one:-two}
shane@mainbox shane $ echo $DATE
two

How would I get it to echo the value one?

Link to post
Share on other sites

I think you want to do arrays.. (bash does one dimention arryas)

here is a quick bash array

bash-2.05b$ cat test
#!/bin/bash
DATE[1]=one
DATE[2]=two
echo ${DATE[1]}
bash-2.05b$    

you could also do this

#!/bin/bash
echo" input an answer"
read inp
echo "input another answer"
read inp2
DATE[1]=$inp
DATE[2]=$inp2
echo ${DATE[1]}
echo ${DATE[2]}

Link to post
Share on other sites
Is ther a way to show(list) all set and/or exported varibles in a bash shell?

$ set

will print all shell variables, including those not exported.

$ printenv

or

$ env

will print exported variables. The latter will also allow you to modify the environment before issuing a command.

I have a substitution question. Lets say I do this

shane@mainbox shane $ DATE=${one:-two}
shane@mainbox shane $ echo $DATE
two

How would I get it to echo the value one?

$ echo one

:)

In the form ${one:-two}, 'one' is the name of a variable, so the value won't be 'one' unless you happen to have

$ one=one

somewhere.

Edited by jcl
Link to post
Share on other sites

wow that set command is handy. Now I am able to kind of see what is happening.

I thought that $DATE would change depnding on what 'one' was set to. It does but 'one' has to be set before doing

DATE=${one:-three}

I was setting 'one' after using the above command. Then It has no effect one the variable DATE.

So long as 'one' is not set before using DATE=${one:-three} three is substituted.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...