LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-08-2014, 03:02 PM   #1
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 536

Rep: Reputation: Disabled
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
Code:
-sh-4.1$ HELLO=Goodby
-sh-4.1$ echo $HELLO
Goodby
-sh-4.1$ export HELLO
-sh-4.1$ echo $HELLO
Goodby
-sh-4.1$

Code:
       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-
              tion.
 
Old 04-08-2014, 03:19 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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:
Code:
$ cat main.c
#include <stdio.h>
#include <stdlib.h>

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

Last edited by suicidaleggroll; 04-08-2014 at 03:22 PM.
 
Old 04-08-2014, 03:25 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.

Last edited by colucix; 04-08-2014 at 03:27 PM.
 
1 members found this post helpful.
Old 04-09-2014, 10:17 AM   #4
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 536

Original Poster
Rep: Reputation: Disabled
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.
 
Old 04-09-2014, 10:47 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
to test it:
Code:
$ HELLO=Goodbye
$ echo $HELLO
Goodbye
$ bash   # << child shell
$ echo $HELLO

$ exit
exit
$ echo $HELLO
Goodbye
$ export HELLO
$ bash   # << child shell
$ echo $HELLO
Goodbye
 
1 members found this post helpful.
Old 04-09-2014, 12:40 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,599

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
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.
 
1 members found this post helpful.
Old 04-09-2014, 07:25 PM   #7
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 536

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Qt Environmental Variables Slackware? FreakboY Programming 3 05-25-2008 04:14 AM
Environmental Variables danglingpointer Slackware 13 01-06-2005 01:36 AM
environmental variables msound Programming 0 08-21-2004 08:06 PM
environmental variables? blackzone Linux - General 1 08-06-2004 04:44 AM
environmental variables mbhenry Linux - General 6 09-18-2003 12:41 PM


All times are GMT -5. The time now is 10:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration