-   Linux - Newbie (
-   -   Environmental variables and export (

NotionCommotion 04-08-2014 03:02 PM

Environmental variables and export
I am following Visual Quickstart Unit and Linux. I see how I can set an environmental variable. I didn't think it should be available until I exported it, but when I tried, I was able to echo it before exporting it. I've read the man file on export, but am still not certain. Can anyone help provide clarification on setting variables and export? Thank you

-sh-4.1$ HELLO=Goodby
-sh-4.1$ echo $HELLO
-sh-4.1$ export HELLO
-sh-4.1$ echo $HELLO


      export -p
              The  supplied names are marked for automatic export to the envi-
              ronment of subsequently executed commands.  If the -f option  is
              given,  the names refer to functions.  If no names are given, or
              if the -p option is supplied, a  list  of  all  names  that  are
              exported  in  this  shell  is printed.  The -n option causes the
              export property to be removed from each  name.  If  a  variable
              name  is  followed by =word, the value of the variable is set to
              word.  export returns an exit status  of  0  unless  an  invalid
              option  is  encountered,  one  of the names is not a valid shell
              variable name, or -f is supplied with a name that is not a func-

suicidaleggroll 04-08-2014 03:19 PM

Without export, you've set a regular shell variable rather than an environment variable.

In your shell there is very little difference (as you can see with your echo), but as soon as you start calling other programs you'll see a difference. Environment variables are available from within any programs you call, regular shell variables are not.

For example:

$ cat main.c
#include <stdio.h>
#include <stdlib.h>

int main()
  char *evar;
  evar = getenv("TESTVAR");
    printf("TESTVAR: %s\n",evar);
  return 0;
$ g++ main.c
$ ./a.out
$ TESTVAR=myvalue
$ echo $TESTVAR
$ ./a.out
$ export TESTVAR
$ ./a.out
TESTVAR: myvalue

colucix 04-08-2014 03:25 PM

When a variable is assigned, its value is stored in memory and you can readily access it in the current shell (this is called a local scope). Problems arise when a sub-process is started from the current (parent) shell, e.g. when you launch a shell script. The sub-process is not aware of the variables stored in memory from the parent shell, unless they've been exported. Exporting a variable means to make it available to every sub-process (global scope), exactly as a resource available from the surrounding environment.

NotionCommotion 04-09-2014 10:17 AM

Thank you,

I previous test and exported a variable from child shell and saw it wasn't set in parent. Guess it only goes the other direction.

pan64 04-09-2014 10:47 AM

to test it:

$ HELLO=Goodbye
$ echo $HELLO
$ bash  # << child shell
$ echo $HELLO

$ exit
$ echo $HELLO
$ export HELLO
$ bash  # << child shell
$ echo $HELLO

jpollard 04-09-2014 12:40 PM

Shell variables are placed in an array of variables. When an export of a shell variable is done it is moved to a list that will be COPIED to the new process (actually, only a pointer is involved).

When a child process is started, the "export" list is copied to the new process (as an exported list which is copied to its child processes). Thus no changes to an exported variable can be seen by the parent process.

NotionCommotion 04-09-2014 07:25 PM

Unfortunately, I do not know C or any derivative of it. But I have messed around with modern versions of PHP and JavaScript, and think I understand the concept. We are just talking about local variables and global PHP variables or JavaScript parent variables. Export is just like PHP's global function. Maybe I have hope :)

PS. Bragging right now. Just skied 28K feet at Whistler today. My legs are dead, and typing while sipping a beer and looking at the mountain.

All times are GMT -5. The time now is 11:54 PM.