linux variables local vs environment and duration

a variable in linux shell.  i'm not really understanding the difference between an environment variable and a shell variable.
how long does either last?  how do you know which one is which?

if I set a variable:

VARIABLE="David" in a shell script
execute the script and go and type echo $VARIABLE... shouldn't it kick out David?

i know there are some variables you can export out of the shell, correct? but, i'm not really interested as i'm concentrating solely on bash at this point? or is that not how shells within linux work?

thanks

i guess now that i think of it ive heard of a single running of a script doing so within a 'shell'.. is this shell unique in it's processor and/or memory utilization?
why would i use anything other than bash? specialized customizations?

-dave.j
DavidCyberNetSec GuyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
General difference is...

1) An environment variable is set at the root shell level, so any child processes inherit these values.

2) Shell variables are generally consider to be specific to one shell process + disappear along with the process.

This is sort of how these terms are used when describing the difference.

And, technically there is no difference.
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
To expand on my comment + maybe answer what you seem to be asking.

Any time you do export foo=val, then $foo is available to all child shells.

Just setting foo=val, means the value (usually) only exists in the current shell, so neither parent shell or any grandchildren shell/processes will see foo in this context...

So the export operator can be though of extending the scope of a shell variable to the entire ancestry of all child processes, shells or otherwise.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nociSoftware EngineerCommented:
All "shell" variables are environment variables.
When a progam is started  a copy  is created using fork.    Then only "export" variables are sent to the created process.

When you run a script it normaly runs in a subsheel and the variable will not be visible in the higher level program.
The exception if you SOURCE a shell script. (The is is technically not "running" independently, it more or less is included..
if you enter exit in a "sourced" script the script logs off the shell process.
Sourcing a script is  
source script
or
. script   #dot space script...

Normaly a script is run just like a program, And it will start the right interpreter (shell) then the first line start with #!/bin.bash   #(for bash)

So i think (from the question) that you confuse sourceing a script vs. running a script/program.
(Running happens in a separate program, that DOES NOT send any data upstream).
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

tel2Commented:
Hi noci,
"(Running happens in a separate program, that DOES NOT send any data upstream)."
Do you mean:
  "...a separate process..."?


Hi David (the David who asked the question):
So you've got one answer saying:
   "An environment variable is set at the root shell level, so any child processes inherit these values."
and one saying:
   'All "shell" variables are environment variables.'
Feel free to flip a coin to work out which is correct.
(Just tell me if I'm being too helpful, here.)
nociSoftware EngineerCommented:
@tel2, ah yes: a separate process space ...  actually.
Steven VonaCommented:
a variable in linux shell.  i'm not really understanding the difference between an environment variable and a shell variable.
how long does either last?  how do you know which one is which?

There really is no difference, a shell variable once exported can become an environmental variable (or part of your environment). Both last until the shell instance they were created in is closed.  If you export a variable, it becomes accessible in all child processes, including spawned shells.

if I set a variable:

VARIABLE="David" in a shell script
execute the script and go and type echo $VARIABLE... shouldn't it kick out David?

You don't need to execute the script, it can all be done on the command line:
[savona@putor ~]$ VARIABLE="David"
[savona@putor ~]$ echo $VARIABLE
David
[savona@putor ~]$

i know there are some variables you can export out of the shell, correct? but, i'm not really interested as i'm concentrating solely on bash at this point? or is that not how shells within linux work?

You can export all variables, in bash or other shells.

thanks

i guess now that i think of it ive heard of a single running of a script doing so within a 'shell'.. is this shell unique in it's processor and/or memory utilization?

A shell is just an interpreter for the system.  Although different shells behave differently, they are no unique to a processor and/or memory.

why would i use anything other than bash? specialized customizations?

Mostly just preference of syntax and feature set.  Bash is most common as it is easily accessible and default on a lot of systems.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.