What exactly is an environment variable? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionEnvironment variables. Where are they saved in?bash script: incorrect argument value being setWhat environment variables/shell variables do env and printenv showGet/Set environment variables in a different ttyCan a shell script be a global command?What scopes can shell variables have?Exporting environment variableDifference between environment variable and shell variableHow do I set the PATH or other environment variables so that X apps can access it?Updating environment variable in a shell scriptBash - shell vs environment variableBash - Shell variable gets promoted to environment variable in child bash processRelation between shell start up processes and environment of parent processGet the latest value of an environment variable is a bash shell scriptWhy my newly created user is not inheriting $PATH variable

How to answer "Have you ever been terminated?"

How to find all the available tools in mac terminal?

How to tell that you are a giant?

Compare a given version number in the form major.minor.build.patch and see if one is less than the other

old style "caution" boxes

Chinese Seal on silk painting - what does it mean?

Trademark violation for app?

Using et al. for a last / senior author rather than for a first author

Is it common practice to audition new musicians one-on-one before rehearsing with the entire band?

What causes the direction of lightning flashes?

Do square wave exist?

How to compare two different files line by line in unix?

First console to have temporary backward compatibility

Do wooden building fires get hotter than 600°C?

Is it fair for a professor to grade us on the possession of past papers?

Wu formula for manifolds with boundary

Crossing US/Canada Border for less than 24 hours

Is safe to use va_start macro with this as parameter?

Why aren't air breathing engines used as small first stages

Is grep documentation wrong?

Closed form of recurrent arithmetic series summation

What is homebrew?

2001: A Space Odyssey's use of the song "Daisy Bell" (Bicycle Built for Two); life imitates art or vice-versa?

What would be the ideal power source for a cybernetic eye?



What exactly is an environment variable?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionEnvironment variables. Where are they saved in?bash script: incorrect argument value being setWhat environment variables/shell variables do env and printenv showGet/Set environment variables in a different ttyCan a shell script be a global command?What scopes can shell variables have?Exporting environment variableDifference between environment variable and shell variableHow do I set the PATH or other environment variables so that X apps can access it?Updating environment variable in a shell scriptBash - shell vs environment variableBash - Shell variable gets promoted to environment variable in child bash processRelation between shell start up processes and environment of parent processGet the latest value of an environment variable is a bash shell scriptWhy my newly created user is not inheriting $PATH variable



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








42















I know that VARIABLE=value creates an environment variable, and export VARIABLE=value makes it available to processes created by the current shell. env shows the current environment variables, but where do they live? What comprises an environment variable (or an environment, for that matter)?










share|improve this question




























    42















    I know that VARIABLE=value creates an environment variable, and export VARIABLE=value makes it available to processes created by the current shell. env shows the current environment variables, but where do they live? What comprises an environment variable (or an environment, for that matter)?










    share|improve this question
























      42












      42








      42


      6






      I know that VARIABLE=value creates an environment variable, and export VARIABLE=value makes it available to processes created by the current shell. env shows the current environment variables, but where do they live? What comprises an environment variable (or an environment, for that matter)?










      share|improve this question














      I know that VARIABLE=value creates an environment variable, and export VARIABLE=value makes it available to processes created by the current shell. env shows the current environment variables, but where do they live? What comprises an environment variable (or an environment, for that matter)?







      bash shell environment-variables






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 18 '13 at 18:32









      MattMatt

      438410




      438410




















          5 Answers
          5






          active

          oldest

          votes


















          29














          An environment is not as magical as it might seem. The shell stores it in memory and passes to the execve() system call. The child process inherits it as an array pointer called environ. From the execve manpage:




          SYNOPSIS



           #include <unistd.h>

          int execve(const char *filename, char *const argv[],
          char *const envp[]);


          argv is an array of argument strings passed to the new program.

          By convention, the first of these strings should contain the filename
          associated with the file being executed. envp is an array of strings,
          conventionally of the form key=value, which are passed as environment
          to the new program.




          The environ(7) manpage also offers some insight:




          SYNOPSIS



           extern char **environ;


          DESCRIPTION



          The variable environ points to an array of pointers to strings
          called the "environment". The last pointer in this array has the
          value NULL. (This variable must be declared in the user program,
          but is declared in the header file <unistd.h> in case the
          header files came from libc4 or libc5, and in case they came from
          glibc and _GNU_SOURCE was defined.) This array of strings is made
          available to the process by the exec(3) call that started the process.




          Both of these GNU manpages match the POSIX specification






          share|improve this answer




















          • 4





            +1 it is probably worth noting that some members of the exec(3) family (i.e. those not matching exec*v) pass **environ under the covers.

            – msw
            Sep 18 '13 at 20:50






          • 5





            Note that it is not about child processes (child processes inherit all the memory of their parent), but executed programs (in the same process), so it's another way to pass data accross a execve() system call (that otherwise wipes the memory of the process).

            – Stéphane Chazelas
            Sep 20 '13 at 20:00











          • @msw: It's the exec*e variants that explicitly pass an env, instead of implicitly using the environ global variable. The v means "vector", and refers to the command line arguments passed as an array (rather than a "list" (variable-length function)) execve is a system call, and all the other exec* functions are libc wrappers for it.

            – Peter Cordes
            Apr 19 '15 at 14:38



















          19














          You've got it just a little wrong: SOME_NAME=value creates a shell variable (in most shells). export SOME_NAME=value creates an environment variable. For better for for worse, most Unix/Linux/*BSD shells use identical syntax in accessing environment variables and shell variables.



          In some larger sense, an "environment" is just the information that goes along with program execution. In C programs, you might find the process ID with a getpid() call, in a shell program you would use a variable access: $$. The process ID is just part of the program's environment. I believe the term "environment" comes from some of the more theoretical computer science topics, like modelling program execution.. Models of program execution have an environment "which contains the associations between variables and their values".



          And this latter, stronger definition is what an "environment" is for Unix/Linux/*BSD shells: an association between names ("variables") and their values. For most Unix-style shells, the values are all character strings, although that's not as strictly true as it used to be. Ksh, Zsh and Bash all have typed variables these days. Even shell function definitions can be exported.



          The use of an environment separate from plain shell variables involves the fork/exec method of starting a new process that all Unixes use. When you export a name/value pair, that name/value pair will be present in the environment of new executables, started by the shell with an execve(2) system call (usually following a fork(2), except when the exec shell command was used).



          Following an execve(), the main() function of new binary has its command line arguments, the environment (stored as a NULL-terminated array of pointers to var=value strings, see the environ(7) man page). Other state that's inherited includes ulimit settings, current working directory, and any open file descriptors that the execve() caller didn't have FD_CLOEXEC set for. The current state of the tty (echo enabled, raw mode, etc.) could also be considered part of the execution state inherited by a newly-execed process.



          See the bash manual's description of the execution environment for simple commands (other than builtin or shell functions).



          Unix environment is different from at least some other operating systems: VMS "lexicals" could be changed by a child process, and that change was visible in the parent. A VMS cd in a child process would affect the working directory of the parent. At least in some circumstances, and my memory may be failing me.



          Some environment variables are well known, $HOME, $PATH, $LD_LIBRARY_PATH and others. Some are conventional to a given programming system, so that a parent shell can pass lots and lots of special-purpose information to some program, like a specific temporary directory, or a user ID and password that don't show up in ps -ef. Simple CGI programs inherit a lot of information from the web server via environment variables, for example.






          share|improve this answer




















          • 1





            It would seem to be a little more complicated still. In bash at least SOME_NAME=value command will set the SOME_NAME environment variable for that command invocation. Confusingly, it doesn't seem to set the shell variable of the same name.

            – Samuel Edwin Ward
            Sep 18 '13 at 19:54







          • 2





            To be more precise, environment variables are not inherited but rather explicitly passed from a shell to programs it spawns.

            – msw
            Sep 18 '13 at 20:44






          • 2





            @SamuelEdwinWard the reason that your SOME_NAME=value command behaves contrary to your expectation is that it is a special syntax meaning "add SOME_NAME to the environment passed to command but do not otherwise alter this shell's variables".

            – msw
            Sep 18 '13 at 20:46







          • 1





            Fascinating, the link to lambda calculus/functional programming. That's an interesting connection which makes a lot of sense.

            – Matt
            Sep 18 '13 at 22:59






          • 1





            Some of this is not quite right. For example, subshells are subprocesses and must be fork()ed, but they do receive (copies of) shell variables.

            – ruakh
            Sep 19 '13 at 5:53


















          7














          Environment variables in their rawest form are just a set of name/value pairs. As described in the bash man page (man 1 bash) under the ENVIRONMENT section:



           When a program is invoked it is given an array of strings called the
          environment. This is a list of name-value pairs, of the form
          name=value.

          The shell provides several ways to manipulate the environment. On
          invocation, the shell scans its own environment and creates a parameter
          for each name found, automatically marking it for export to child pro-
          cesses. Executed commands inherit the environment.


          In practical terms, it allows you to define behavior that is shared or unique to programs invoked from the present shell. For example, when using crontab or visudo you can define the EDITOR environment variable to define another editor other than the one your system would use by default. The same can be held true for things like the man command which looks at your PAGER environment to work out what pager program should be used to display the output of the man page with.



          Quite a lot of unix commands read the environment and depending on what is set there alter their output/processing/action depending on these. Some are shared, some are unique to the program. Most man pages contain information on how the environment variable have an effect on the described program.



          Other practical illustrations are for things such as systems with several installs of Oracle on the same platform. By setting ORACLE_HOME, the whole suite of oracle commands (as loaded from your PATH environment variable) then pull settings, definitions, mappings and libraries from under that top level directory. The same hold true for other programs such as java with it's JAVA_HOME environment variable.



          bash itself has many environment variables which can change the behavior of a range of things from history (HISTSIZE, HISTFILE etc), screen size (COLUMNS), tab completion (FIGNORE,GLOBIGNORE) locale and character encoding/decoding (LANG, LC_*), prompt (PS1 .. PS4), and so forth (again seek knowledge from the bash man page).



          Also you can write scripts/programs that make use of your own custom environment variables (to pass settings, or change functionality).






          share|improve this answer
































            0














            "Environment Variables" are a set of dynamic named values that can affect the way running processes will behave on a computer.



            They are part of the operating environment in which a process runs. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME or USERPROFILE variable to find the directory structure owned by the user running the process.



            More info here &rightarrow; http://en.wikipedia.org/wiki/Environment_variable.



            Everything you want to know about Environment Variables... &uparrow;






            share|improve this answer




















            • 1





              Although it is unlikely that this links goes away, it is better to answer the question here with relevant text and provide the link as an addition for backup information.

              – Anthon
              Sep 19 '13 at 7:22











            • @Anthon I do believe you are correct and I will make the changes as soon as I can... Thanks for the advise...

              – SoCalDiegoRob
              Sep 19 '13 at 8:37


















            -1














            This answer requires some shell scripting experience and knowledge with the terms variable, value, variable substitution, prompt, echo, kernel, shell, utility, session and process.



            An environment variable (envar) is a set of global defined variables that can effect the way a given processes will behave on a computer's operating system.



            1. An exemplary introduction:



            We substitute envars with a $ and capitalized letters. For example: $PS1.



            We can print an envar this way:



            echo $PS1


            $PS1 holds the value of the Unix prompt. Say its native values are u w $.




            • u stands for (current) user,


            • w stands for working directory,


            • $ is to border the prompt.

            So, if we do: echo $PS1, we see the values of u, w plus the dollar sign in the end.



            We could change the Unix behavior in that context, if we change the values of that envar. For example:



            PS1="w >"


            Now the prompt looks like this (assuming the work directory is named "John"):



            John >


            In the same manner we could do PS1="Hello, I'm your prompt >", so echo $PS1 will bring:



            Hello, I'm your prompt >


            In Bash 4.x.x, we can print ALL envars in the system with the env command. I suggest executing env in the terminal and take some look at the output.



            2. How are these data shown and manipulated:



            The terminal of a session let's us to customize the envars that are coming with Bash.



            The aforementioned changes are usually temporary, and here's why:



            Each session (which isn't a sub-session) is unique, and several processes can run uniquely at the same time (each with its own set of envars) but usually there is inheritance from session 0 to session 1 and upwards.



            Changes we make to one process are unique to it, and will cease if we close it without saving them in some way.



            So how can we save these changes:



            There are several types of ways available to store envar changes, depending on the scope we pick. Here are different scopes (levels) for such changes:



            • Process level: The envars are only available for programs in the current session.

            • Export level: The envars are available for programs in the current session, or all its sub-sessions.

            • Global level: The changes will be stored for all sessions whatsoever (primary and all subs).

            Where are envar data stored:



            Unix is built of 3 main layers: Kernel, shell, and utilities. AFAIK each shell has its own envars, and these are built primarily or exclusively in the shell.



            The specific location in which to globally change these is usually /etc/profile though we can also do that in .bashrc of course.



            3. Creating new envars:



            We can create new envars and here is a way; as of Bash 4.x.x there is no native enavar named MESSAGE (as said, envars are usually uppercased).



            MESSAGE="Hello world!"


            will create it for us, and now if we type echo $MESSAGE, we get hello world!.



            If we'll execute bash in our current working session (window), we would start a new bash sub-session and will no longer work in the original process, unless we execute exit.



            Note: In operating systems with a terminal emulator (like Ubuntu desktop), a sub-session usually runs on the same window, but a new session in another window isn't a sub-session of the existing one (it's an adjacent process).



            Note: Don't use special signs in envar values such as ! or they won't be saved.



            Exporting the envar from the original session to all sub-sessions:



            We can still use the envar created in the first session, in the second one as well, without registering it in the user or global level conf files (see following data). Here's how to do that:



            Go to the original session (whether on the current window or another) and execute:



            export MESSAGE


            when exporting, don't use a $ sign.



            It is now exported to all sub-sessions. If you'll do echo $MESSAGE on a sub-session, whether from your user or another, it will then be printed.



            Note that Shell internal variables such as PS1 should not be exported, but if you do want to export them from whatever reason and they don't appear, don't execute bash after export, but rather bash –norc.



            4. The $PATH envar:



            $PATH is the envar that users will usually change the most.



            If we echo $PATH, we are going to see this stream:



            /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games


            The printed values of this envar are separated by colons (:) there, but here's a potentially more comfortable way (these are the same values):



            /usr/local/bin
            /usr/bin
            /bin
            /usr/local/games
            /usr/games


            These are direcotries to search for, when we run a utility.



            By executing which echo we will get its file location - for example, we might see it exists in /bin/echo.



            Based on that we don't have to type echo envar to view the evnar's values. We can also do:



            /bin/echo $ENVAR


            The envar will still be executed, for example:



            /bin/echo $HOME


            Gives us



            /home/User || /root


            Just as:



            echo $HOME


            Gives us



            /home/User || /root


            Note: $HOME is abbreviated as ~.



            The system-$PATH relations, and a possible user interaction:



            In Bash 4.x.x, when we use a utility without its full path, the system will use all 6 values mentioned above, of the $PATH envar. So, it will start from /user/local/bin, and will follow all its content looking for the echo executable.



            In this case, it will stop at /bin/echo, in which, in this case, the executable resides.



            Hence, the main reason we might customize the $PATH envar, is installing executables that are not under any of its native values.



            After installing such executables, we should set their $PATH value accordingly and then we would be able to work with them.



            5. Appendix - expanding $PATH:



            We can export $PATH to bash sub-sessions (that includes bash extensions like WP-CLI for WordPress or Drush for Drupal ) this way:



            export PATH="/home/John:$PATH"


            This will add a new value /home/John to $PATH, and then right afterwards, it will annex any native values to it (right after the colon), which are stored under the syntax $PATH.



            Such permanent change can be done in the relevant script, usually under /etc/profile and by the name .bashrc.






            share|improve this answer




















            • 3





              There is an awful lot wrong with this answer: conflation of sessions and processes, warning about ! in an environment variable value not working that is right below an example showing it working, a false notion of sub-sessions, quite bizarre advice about what to do after exporting a shell variable, and a false notion of global environment variables.

              – JdeBP
              Nov 1 '18 at 17:34











            • warning about ! in an environment variable value not working that is right below an example showing it working? Please example.

              – JohnDoea
              Nov 1 '18 at 17:46











            • quite bizarre advice about what to do after exporting a shell variable, what exactly do you mean?

              – JohnDoea
              Nov 1 '18 at 17:47












            • false notion of global environment variables, what exactly do you mean?

              – JohnDoea
              Nov 1 '18 at 17:48











            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f91282%2fwhat-exactly-is-an-environment-variable%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            29














            An environment is not as magical as it might seem. The shell stores it in memory and passes to the execve() system call. The child process inherits it as an array pointer called environ. From the execve manpage:




            SYNOPSIS



             #include <unistd.h>

            int execve(const char *filename, char *const argv[],
            char *const envp[]);


            argv is an array of argument strings passed to the new program.

            By convention, the first of these strings should contain the filename
            associated with the file being executed. envp is an array of strings,
            conventionally of the form key=value, which are passed as environment
            to the new program.




            The environ(7) manpage also offers some insight:




            SYNOPSIS



             extern char **environ;


            DESCRIPTION



            The variable environ points to an array of pointers to strings
            called the "environment". The last pointer in this array has the
            value NULL. (This variable must be declared in the user program,
            but is declared in the header file <unistd.h> in case the
            header files came from libc4 or libc5, and in case they came from
            glibc and _GNU_SOURCE was defined.) This array of strings is made
            available to the process by the exec(3) call that started the process.




            Both of these GNU manpages match the POSIX specification






            share|improve this answer




















            • 4





              +1 it is probably worth noting that some members of the exec(3) family (i.e. those not matching exec*v) pass **environ under the covers.

              – msw
              Sep 18 '13 at 20:50






            • 5





              Note that it is not about child processes (child processes inherit all the memory of their parent), but executed programs (in the same process), so it's another way to pass data accross a execve() system call (that otherwise wipes the memory of the process).

              – Stéphane Chazelas
              Sep 20 '13 at 20:00











            • @msw: It's the exec*e variants that explicitly pass an env, instead of implicitly using the environ global variable. The v means "vector", and refers to the command line arguments passed as an array (rather than a "list" (variable-length function)) execve is a system call, and all the other exec* functions are libc wrappers for it.

              – Peter Cordes
              Apr 19 '15 at 14:38
















            29














            An environment is not as magical as it might seem. The shell stores it in memory and passes to the execve() system call. The child process inherits it as an array pointer called environ. From the execve manpage:




            SYNOPSIS



             #include <unistd.h>

            int execve(const char *filename, char *const argv[],
            char *const envp[]);


            argv is an array of argument strings passed to the new program.

            By convention, the first of these strings should contain the filename
            associated with the file being executed. envp is an array of strings,
            conventionally of the form key=value, which are passed as environment
            to the new program.




            The environ(7) manpage also offers some insight:




            SYNOPSIS



             extern char **environ;


            DESCRIPTION



            The variable environ points to an array of pointers to strings
            called the "environment". The last pointer in this array has the
            value NULL. (This variable must be declared in the user program,
            but is declared in the header file <unistd.h> in case the
            header files came from libc4 or libc5, and in case they came from
            glibc and _GNU_SOURCE was defined.) This array of strings is made
            available to the process by the exec(3) call that started the process.




            Both of these GNU manpages match the POSIX specification






            share|improve this answer




















            • 4





              +1 it is probably worth noting that some members of the exec(3) family (i.e. those not matching exec*v) pass **environ under the covers.

              – msw
              Sep 18 '13 at 20:50






            • 5





              Note that it is not about child processes (child processes inherit all the memory of their parent), but executed programs (in the same process), so it's another way to pass data accross a execve() system call (that otherwise wipes the memory of the process).

              – Stéphane Chazelas
              Sep 20 '13 at 20:00











            • @msw: It's the exec*e variants that explicitly pass an env, instead of implicitly using the environ global variable. The v means "vector", and refers to the command line arguments passed as an array (rather than a "list" (variable-length function)) execve is a system call, and all the other exec* functions are libc wrappers for it.

              – Peter Cordes
              Apr 19 '15 at 14:38














            29












            29








            29







            An environment is not as magical as it might seem. The shell stores it in memory and passes to the execve() system call. The child process inherits it as an array pointer called environ. From the execve manpage:




            SYNOPSIS



             #include <unistd.h>

            int execve(const char *filename, char *const argv[],
            char *const envp[]);


            argv is an array of argument strings passed to the new program.

            By convention, the first of these strings should contain the filename
            associated with the file being executed. envp is an array of strings,
            conventionally of the form key=value, which are passed as environment
            to the new program.




            The environ(7) manpage also offers some insight:




            SYNOPSIS



             extern char **environ;


            DESCRIPTION



            The variable environ points to an array of pointers to strings
            called the "environment". The last pointer in this array has the
            value NULL. (This variable must be declared in the user program,
            but is declared in the header file <unistd.h> in case the
            header files came from libc4 or libc5, and in case they came from
            glibc and _GNU_SOURCE was defined.) This array of strings is made
            available to the process by the exec(3) call that started the process.




            Both of these GNU manpages match the POSIX specification






            share|improve this answer















            An environment is not as magical as it might seem. The shell stores it in memory and passes to the execve() system call. The child process inherits it as an array pointer called environ. From the execve manpage:




            SYNOPSIS



             #include <unistd.h>

            int execve(const char *filename, char *const argv[],
            char *const envp[]);


            argv is an array of argument strings passed to the new program.

            By convention, the first of these strings should contain the filename
            associated with the file being executed. envp is an array of strings,
            conventionally of the form key=value, which are passed as environment
            to the new program.




            The environ(7) manpage also offers some insight:




            SYNOPSIS



             extern char **environ;


            DESCRIPTION



            The variable environ points to an array of pointers to strings
            called the "environment". The last pointer in this array has the
            value NULL. (This variable must be declared in the user program,
            but is declared in the header file <unistd.h> in case the
            header files came from libc4 or libc5, and in case they came from
            glibc and _GNU_SOURCE was defined.) This array of strings is made
            available to the process by the exec(3) call that started the process.




            Both of these GNU manpages match the POSIX specification







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 19 '13 at 11:29









            Joachim Sauer

            1,270185




            1,270185










            answered Sep 18 '13 at 18:46









            jordanmjordanm

            31.4k38897




            31.4k38897







            • 4





              +1 it is probably worth noting that some members of the exec(3) family (i.e. those not matching exec*v) pass **environ under the covers.

              – msw
              Sep 18 '13 at 20:50






            • 5





              Note that it is not about child processes (child processes inherit all the memory of their parent), but executed programs (in the same process), so it's another way to pass data accross a execve() system call (that otherwise wipes the memory of the process).

              – Stéphane Chazelas
              Sep 20 '13 at 20:00











            • @msw: It's the exec*e variants that explicitly pass an env, instead of implicitly using the environ global variable. The v means "vector", and refers to the command line arguments passed as an array (rather than a "list" (variable-length function)) execve is a system call, and all the other exec* functions are libc wrappers for it.

              – Peter Cordes
              Apr 19 '15 at 14:38













            • 4





              +1 it is probably worth noting that some members of the exec(3) family (i.e. those not matching exec*v) pass **environ under the covers.

              – msw
              Sep 18 '13 at 20:50






            • 5





              Note that it is not about child processes (child processes inherit all the memory of their parent), but executed programs (in the same process), so it's another way to pass data accross a execve() system call (that otherwise wipes the memory of the process).

              – Stéphane Chazelas
              Sep 20 '13 at 20:00











            • @msw: It's the exec*e variants that explicitly pass an env, instead of implicitly using the environ global variable. The v means "vector", and refers to the command line arguments passed as an array (rather than a "list" (variable-length function)) execve is a system call, and all the other exec* functions are libc wrappers for it.

              – Peter Cordes
              Apr 19 '15 at 14:38








            4




            4





            +1 it is probably worth noting that some members of the exec(3) family (i.e. those not matching exec*v) pass **environ under the covers.

            – msw
            Sep 18 '13 at 20:50





            +1 it is probably worth noting that some members of the exec(3) family (i.e. those not matching exec*v) pass **environ under the covers.

            – msw
            Sep 18 '13 at 20:50




            5




            5





            Note that it is not about child processes (child processes inherit all the memory of their parent), but executed programs (in the same process), so it's another way to pass data accross a execve() system call (that otherwise wipes the memory of the process).

            – Stéphane Chazelas
            Sep 20 '13 at 20:00





            Note that it is not about child processes (child processes inherit all the memory of their parent), but executed programs (in the same process), so it's another way to pass data accross a execve() system call (that otherwise wipes the memory of the process).

            – Stéphane Chazelas
            Sep 20 '13 at 20:00













            @msw: It's the exec*e variants that explicitly pass an env, instead of implicitly using the environ global variable. The v means "vector", and refers to the command line arguments passed as an array (rather than a "list" (variable-length function)) execve is a system call, and all the other exec* functions are libc wrappers for it.

            – Peter Cordes
            Apr 19 '15 at 14:38






            @msw: It's the exec*e variants that explicitly pass an env, instead of implicitly using the environ global variable. The v means "vector", and refers to the command line arguments passed as an array (rather than a "list" (variable-length function)) execve is a system call, and all the other exec* functions are libc wrappers for it.

            – Peter Cordes
            Apr 19 '15 at 14:38














            19














            You've got it just a little wrong: SOME_NAME=value creates a shell variable (in most shells). export SOME_NAME=value creates an environment variable. For better for for worse, most Unix/Linux/*BSD shells use identical syntax in accessing environment variables and shell variables.



            In some larger sense, an "environment" is just the information that goes along with program execution. In C programs, you might find the process ID with a getpid() call, in a shell program you would use a variable access: $$. The process ID is just part of the program's environment. I believe the term "environment" comes from some of the more theoretical computer science topics, like modelling program execution.. Models of program execution have an environment "which contains the associations between variables and their values".



            And this latter, stronger definition is what an "environment" is for Unix/Linux/*BSD shells: an association between names ("variables") and their values. For most Unix-style shells, the values are all character strings, although that's not as strictly true as it used to be. Ksh, Zsh and Bash all have typed variables these days. Even shell function definitions can be exported.



            The use of an environment separate from plain shell variables involves the fork/exec method of starting a new process that all Unixes use. When you export a name/value pair, that name/value pair will be present in the environment of new executables, started by the shell with an execve(2) system call (usually following a fork(2), except when the exec shell command was used).



            Following an execve(), the main() function of new binary has its command line arguments, the environment (stored as a NULL-terminated array of pointers to var=value strings, see the environ(7) man page). Other state that's inherited includes ulimit settings, current working directory, and any open file descriptors that the execve() caller didn't have FD_CLOEXEC set for. The current state of the tty (echo enabled, raw mode, etc.) could also be considered part of the execution state inherited by a newly-execed process.



            See the bash manual's description of the execution environment for simple commands (other than builtin or shell functions).



            Unix environment is different from at least some other operating systems: VMS "lexicals" could be changed by a child process, and that change was visible in the parent. A VMS cd in a child process would affect the working directory of the parent. At least in some circumstances, and my memory may be failing me.



            Some environment variables are well known, $HOME, $PATH, $LD_LIBRARY_PATH and others. Some are conventional to a given programming system, so that a parent shell can pass lots and lots of special-purpose information to some program, like a specific temporary directory, or a user ID and password that don't show up in ps -ef. Simple CGI programs inherit a lot of information from the web server via environment variables, for example.






            share|improve this answer




















            • 1





              It would seem to be a little more complicated still. In bash at least SOME_NAME=value command will set the SOME_NAME environment variable for that command invocation. Confusingly, it doesn't seem to set the shell variable of the same name.

              – Samuel Edwin Ward
              Sep 18 '13 at 19:54







            • 2





              To be more precise, environment variables are not inherited but rather explicitly passed from a shell to programs it spawns.

              – msw
              Sep 18 '13 at 20:44






            • 2





              @SamuelEdwinWard the reason that your SOME_NAME=value command behaves contrary to your expectation is that it is a special syntax meaning "add SOME_NAME to the environment passed to command but do not otherwise alter this shell's variables".

              – msw
              Sep 18 '13 at 20:46







            • 1





              Fascinating, the link to lambda calculus/functional programming. That's an interesting connection which makes a lot of sense.

              – Matt
              Sep 18 '13 at 22:59






            • 1





              Some of this is not quite right. For example, subshells are subprocesses and must be fork()ed, but they do receive (copies of) shell variables.

              – ruakh
              Sep 19 '13 at 5:53















            19














            You've got it just a little wrong: SOME_NAME=value creates a shell variable (in most shells). export SOME_NAME=value creates an environment variable. For better for for worse, most Unix/Linux/*BSD shells use identical syntax in accessing environment variables and shell variables.



            In some larger sense, an "environment" is just the information that goes along with program execution. In C programs, you might find the process ID with a getpid() call, in a shell program you would use a variable access: $$. The process ID is just part of the program's environment. I believe the term "environment" comes from some of the more theoretical computer science topics, like modelling program execution.. Models of program execution have an environment "which contains the associations between variables and their values".



            And this latter, stronger definition is what an "environment" is for Unix/Linux/*BSD shells: an association between names ("variables") and their values. For most Unix-style shells, the values are all character strings, although that's not as strictly true as it used to be. Ksh, Zsh and Bash all have typed variables these days. Even shell function definitions can be exported.



            The use of an environment separate from plain shell variables involves the fork/exec method of starting a new process that all Unixes use. When you export a name/value pair, that name/value pair will be present in the environment of new executables, started by the shell with an execve(2) system call (usually following a fork(2), except when the exec shell command was used).



            Following an execve(), the main() function of new binary has its command line arguments, the environment (stored as a NULL-terminated array of pointers to var=value strings, see the environ(7) man page). Other state that's inherited includes ulimit settings, current working directory, and any open file descriptors that the execve() caller didn't have FD_CLOEXEC set for. The current state of the tty (echo enabled, raw mode, etc.) could also be considered part of the execution state inherited by a newly-execed process.



            See the bash manual's description of the execution environment for simple commands (other than builtin or shell functions).



            Unix environment is different from at least some other operating systems: VMS "lexicals" could be changed by a child process, and that change was visible in the parent. A VMS cd in a child process would affect the working directory of the parent. At least in some circumstances, and my memory may be failing me.



            Some environment variables are well known, $HOME, $PATH, $LD_LIBRARY_PATH and others. Some are conventional to a given programming system, so that a parent shell can pass lots and lots of special-purpose information to some program, like a specific temporary directory, or a user ID and password that don't show up in ps -ef. Simple CGI programs inherit a lot of information from the web server via environment variables, for example.






            share|improve this answer




















            • 1





              It would seem to be a little more complicated still. In bash at least SOME_NAME=value command will set the SOME_NAME environment variable for that command invocation. Confusingly, it doesn't seem to set the shell variable of the same name.

              – Samuel Edwin Ward
              Sep 18 '13 at 19:54







            • 2





              To be more precise, environment variables are not inherited but rather explicitly passed from a shell to programs it spawns.

              – msw
              Sep 18 '13 at 20:44






            • 2





              @SamuelEdwinWard the reason that your SOME_NAME=value command behaves contrary to your expectation is that it is a special syntax meaning "add SOME_NAME to the environment passed to command but do not otherwise alter this shell's variables".

              – msw
              Sep 18 '13 at 20:46







            • 1





              Fascinating, the link to lambda calculus/functional programming. That's an interesting connection which makes a lot of sense.

              – Matt
              Sep 18 '13 at 22:59






            • 1





              Some of this is not quite right. For example, subshells are subprocesses and must be fork()ed, but they do receive (copies of) shell variables.

              – ruakh
              Sep 19 '13 at 5:53













            19












            19








            19







            You've got it just a little wrong: SOME_NAME=value creates a shell variable (in most shells). export SOME_NAME=value creates an environment variable. For better for for worse, most Unix/Linux/*BSD shells use identical syntax in accessing environment variables and shell variables.



            In some larger sense, an "environment" is just the information that goes along with program execution. In C programs, you might find the process ID with a getpid() call, in a shell program you would use a variable access: $$. The process ID is just part of the program's environment. I believe the term "environment" comes from some of the more theoretical computer science topics, like modelling program execution.. Models of program execution have an environment "which contains the associations between variables and their values".



            And this latter, stronger definition is what an "environment" is for Unix/Linux/*BSD shells: an association between names ("variables") and their values. For most Unix-style shells, the values are all character strings, although that's not as strictly true as it used to be. Ksh, Zsh and Bash all have typed variables these days. Even shell function definitions can be exported.



            The use of an environment separate from plain shell variables involves the fork/exec method of starting a new process that all Unixes use. When you export a name/value pair, that name/value pair will be present in the environment of new executables, started by the shell with an execve(2) system call (usually following a fork(2), except when the exec shell command was used).



            Following an execve(), the main() function of new binary has its command line arguments, the environment (stored as a NULL-terminated array of pointers to var=value strings, see the environ(7) man page). Other state that's inherited includes ulimit settings, current working directory, and any open file descriptors that the execve() caller didn't have FD_CLOEXEC set for. The current state of the tty (echo enabled, raw mode, etc.) could also be considered part of the execution state inherited by a newly-execed process.



            See the bash manual's description of the execution environment for simple commands (other than builtin or shell functions).



            Unix environment is different from at least some other operating systems: VMS "lexicals" could be changed by a child process, and that change was visible in the parent. A VMS cd in a child process would affect the working directory of the parent. At least in some circumstances, and my memory may be failing me.



            Some environment variables are well known, $HOME, $PATH, $LD_LIBRARY_PATH and others. Some are conventional to a given programming system, so that a parent shell can pass lots and lots of special-purpose information to some program, like a specific temporary directory, or a user ID and password that don't show up in ps -ef. Simple CGI programs inherit a lot of information from the web server via environment variables, for example.






            share|improve this answer















            You've got it just a little wrong: SOME_NAME=value creates a shell variable (in most shells). export SOME_NAME=value creates an environment variable. For better for for worse, most Unix/Linux/*BSD shells use identical syntax in accessing environment variables and shell variables.



            In some larger sense, an "environment" is just the information that goes along with program execution. In C programs, you might find the process ID with a getpid() call, in a shell program you would use a variable access: $$. The process ID is just part of the program's environment. I believe the term "environment" comes from some of the more theoretical computer science topics, like modelling program execution.. Models of program execution have an environment "which contains the associations between variables and their values".



            And this latter, stronger definition is what an "environment" is for Unix/Linux/*BSD shells: an association between names ("variables") and their values. For most Unix-style shells, the values are all character strings, although that's not as strictly true as it used to be. Ksh, Zsh and Bash all have typed variables these days. Even shell function definitions can be exported.



            The use of an environment separate from plain shell variables involves the fork/exec method of starting a new process that all Unixes use. When you export a name/value pair, that name/value pair will be present in the environment of new executables, started by the shell with an execve(2) system call (usually following a fork(2), except when the exec shell command was used).



            Following an execve(), the main() function of new binary has its command line arguments, the environment (stored as a NULL-terminated array of pointers to var=value strings, see the environ(7) man page). Other state that's inherited includes ulimit settings, current working directory, and any open file descriptors that the execve() caller didn't have FD_CLOEXEC set for. The current state of the tty (echo enabled, raw mode, etc.) could also be considered part of the execution state inherited by a newly-execed process.



            See the bash manual's description of the execution environment for simple commands (other than builtin or shell functions).



            Unix environment is different from at least some other operating systems: VMS "lexicals" could be changed by a child process, and that change was visible in the parent. A VMS cd in a child process would affect the working directory of the parent. At least in some circumstances, and my memory may be failing me.



            Some environment variables are well known, $HOME, $PATH, $LD_LIBRARY_PATH and others. Some are conventional to a given programming system, so that a parent shell can pass lots and lots of special-purpose information to some program, like a specific temporary directory, or a user ID and password that don't show up in ps -ef. Simple CGI programs inherit a lot of information from the web server via environment variables, for example.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 19 '15 at 16:44









            Peter Cordes

            4,5831434




            4,5831434










            answered Sep 18 '13 at 18:54









            Bruce EdigerBruce Ediger

            35.7k670120




            35.7k670120







            • 1





              It would seem to be a little more complicated still. In bash at least SOME_NAME=value command will set the SOME_NAME environment variable for that command invocation. Confusingly, it doesn't seem to set the shell variable of the same name.

              – Samuel Edwin Ward
              Sep 18 '13 at 19:54







            • 2





              To be more precise, environment variables are not inherited but rather explicitly passed from a shell to programs it spawns.

              – msw
              Sep 18 '13 at 20:44






            • 2





              @SamuelEdwinWard the reason that your SOME_NAME=value command behaves contrary to your expectation is that it is a special syntax meaning "add SOME_NAME to the environment passed to command but do not otherwise alter this shell's variables".

              – msw
              Sep 18 '13 at 20:46







            • 1





              Fascinating, the link to lambda calculus/functional programming. That's an interesting connection which makes a lot of sense.

              – Matt
              Sep 18 '13 at 22:59






            • 1





              Some of this is not quite right. For example, subshells are subprocesses and must be fork()ed, but they do receive (copies of) shell variables.

              – ruakh
              Sep 19 '13 at 5:53












            • 1





              It would seem to be a little more complicated still. In bash at least SOME_NAME=value command will set the SOME_NAME environment variable for that command invocation. Confusingly, it doesn't seem to set the shell variable of the same name.

              – Samuel Edwin Ward
              Sep 18 '13 at 19:54







            • 2





              To be more precise, environment variables are not inherited but rather explicitly passed from a shell to programs it spawns.

              – msw
              Sep 18 '13 at 20:44






            • 2





              @SamuelEdwinWard the reason that your SOME_NAME=value command behaves contrary to your expectation is that it is a special syntax meaning "add SOME_NAME to the environment passed to command but do not otherwise alter this shell's variables".

              – msw
              Sep 18 '13 at 20:46







            • 1





              Fascinating, the link to lambda calculus/functional programming. That's an interesting connection which makes a lot of sense.

              – Matt
              Sep 18 '13 at 22:59






            • 1





              Some of this is not quite right. For example, subshells are subprocesses and must be fork()ed, but they do receive (copies of) shell variables.

              – ruakh
              Sep 19 '13 at 5:53







            1




            1





            It would seem to be a little more complicated still. In bash at least SOME_NAME=value command will set the SOME_NAME environment variable for that command invocation. Confusingly, it doesn't seem to set the shell variable of the same name.

            – Samuel Edwin Ward
            Sep 18 '13 at 19:54






            It would seem to be a little more complicated still. In bash at least SOME_NAME=value command will set the SOME_NAME environment variable for that command invocation. Confusingly, it doesn't seem to set the shell variable of the same name.

            – Samuel Edwin Ward
            Sep 18 '13 at 19:54





            2




            2





            To be more precise, environment variables are not inherited but rather explicitly passed from a shell to programs it spawns.

            – msw
            Sep 18 '13 at 20:44





            To be more precise, environment variables are not inherited but rather explicitly passed from a shell to programs it spawns.

            – msw
            Sep 18 '13 at 20:44




            2




            2





            @SamuelEdwinWard the reason that your SOME_NAME=value command behaves contrary to your expectation is that it is a special syntax meaning "add SOME_NAME to the environment passed to command but do not otherwise alter this shell's variables".

            – msw
            Sep 18 '13 at 20:46






            @SamuelEdwinWard the reason that your SOME_NAME=value command behaves contrary to your expectation is that it is a special syntax meaning "add SOME_NAME to the environment passed to command but do not otherwise alter this shell's variables".

            – msw
            Sep 18 '13 at 20:46





            1




            1





            Fascinating, the link to lambda calculus/functional programming. That's an interesting connection which makes a lot of sense.

            – Matt
            Sep 18 '13 at 22:59





            Fascinating, the link to lambda calculus/functional programming. That's an interesting connection which makes a lot of sense.

            – Matt
            Sep 18 '13 at 22:59




            1




            1





            Some of this is not quite right. For example, subshells are subprocesses and must be fork()ed, but they do receive (copies of) shell variables.

            – ruakh
            Sep 19 '13 at 5:53





            Some of this is not quite right. For example, subshells are subprocesses and must be fork()ed, but they do receive (copies of) shell variables.

            – ruakh
            Sep 19 '13 at 5:53











            7














            Environment variables in their rawest form are just a set of name/value pairs. As described in the bash man page (man 1 bash) under the ENVIRONMENT section:



             When a program is invoked it is given an array of strings called the
            environment. This is a list of name-value pairs, of the form
            name=value.

            The shell provides several ways to manipulate the environment. On
            invocation, the shell scans its own environment and creates a parameter
            for each name found, automatically marking it for export to child pro-
            cesses. Executed commands inherit the environment.


            In practical terms, it allows you to define behavior that is shared or unique to programs invoked from the present shell. For example, when using crontab or visudo you can define the EDITOR environment variable to define another editor other than the one your system would use by default. The same can be held true for things like the man command which looks at your PAGER environment to work out what pager program should be used to display the output of the man page with.



            Quite a lot of unix commands read the environment and depending on what is set there alter their output/processing/action depending on these. Some are shared, some are unique to the program. Most man pages contain information on how the environment variable have an effect on the described program.



            Other practical illustrations are for things such as systems with several installs of Oracle on the same platform. By setting ORACLE_HOME, the whole suite of oracle commands (as loaded from your PATH environment variable) then pull settings, definitions, mappings and libraries from under that top level directory. The same hold true for other programs such as java with it's JAVA_HOME environment variable.



            bash itself has many environment variables which can change the behavior of a range of things from history (HISTSIZE, HISTFILE etc), screen size (COLUMNS), tab completion (FIGNORE,GLOBIGNORE) locale and character encoding/decoding (LANG, LC_*), prompt (PS1 .. PS4), and so forth (again seek knowledge from the bash man page).



            Also you can write scripts/programs that make use of your own custom environment variables (to pass settings, or change functionality).






            share|improve this answer





























              7














              Environment variables in their rawest form are just a set of name/value pairs. As described in the bash man page (man 1 bash) under the ENVIRONMENT section:



               When a program is invoked it is given an array of strings called the
              environment. This is a list of name-value pairs, of the form
              name=value.

              The shell provides several ways to manipulate the environment. On
              invocation, the shell scans its own environment and creates a parameter
              for each name found, automatically marking it for export to child pro-
              cesses. Executed commands inherit the environment.


              In practical terms, it allows you to define behavior that is shared or unique to programs invoked from the present shell. For example, when using crontab or visudo you can define the EDITOR environment variable to define another editor other than the one your system would use by default. The same can be held true for things like the man command which looks at your PAGER environment to work out what pager program should be used to display the output of the man page with.



              Quite a lot of unix commands read the environment and depending on what is set there alter their output/processing/action depending on these. Some are shared, some are unique to the program. Most man pages contain information on how the environment variable have an effect on the described program.



              Other practical illustrations are for things such as systems with several installs of Oracle on the same platform. By setting ORACLE_HOME, the whole suite of oracle commands (as loaded from your PATH environment variable) then pull settings, definitions, mappings and libraries from under that top level directory. The same hold true for other programs such as java with it's JAVA_HOME environment variable.



              bash itself has many environment variables which can change the behavior of a range of things from history (HISTSIZE, HISTFILE etc), screen size (COLUMNS), tab completion (FIGNORE,GLOBIGNORE) locale and character encoding/decoding (LANG, LC_*), prompt (PS1 .. PS4), and so forth (again seek knowledge from the bash man page).



              Also you can write scripts/programs that make use of your own custom environment variables (to pass settings, or change functionality).






              share|improve this answer



























                7












                7








                7







                Environment variables in their rawest form are just a set of name/value pairs. As described in the bash man page (man 1 bash) under the ENVIRONMENT section:



                 When a program is invoked it is given an array of strings called the
                environment. This is a list of name-value pairs, of the form
                name=value.

                The shell provides several ways to manipulate the environment. On
                invocation, the shell scans its own environment and creates a parameter
                for each name found, automatically marking it for export to child pro-
                cesses. Executed commands inherit the environment.


                In practical terms, it allows you to define behavior that is shared or unique to programs invoked from the present shell. For example, when using crontab or visudo you can define the EDITOR environment variable to define another editor other than the one your system would use by default. The same can be held true for things like the man command which looks at your PAGER environment to work out what pager program should be used to display the output of the man page with.



                Quite a lot of unix commands read the environment and depending on what is set there alter their output/processing/action depending on these. Some are shared, some are unique to the program. Most man pages contain information on how the environment variable have an effect on the described program.



                Other practical illustrations are for things such as systems with several installs of Oracle on the same platform. By setting ORACLE_HOME, the whole suite of oracle commands (as loaded from your PATH environment variable) then pull settings, definitions, mappings and libraries from under that top level directory. The same hold true for other programs such as java with it's JAVA_HOME environment variable.



                bash itself has many environment variables which can change the behavior of a range of things from history (HISTSIZE, HISTFILE etc), screen size (COLUMNS), tab completion (FIGNORE,GLOBIGNORE) locale and character encoding/decoding (LANG, LC_*), prompt (PS1 .. PS4), and so forth (again seek knowledge from the bash man page).



                Also you can write scripts/programs that make use of your own custom environment variables (to pass settings, or change functionality).






                share|improve this answer















                Environment variables in their rawest form are just a set of name/value pairs. As described in the bash man page (man 1 bash) under the ENVIRONMENT section:



                 When a program is invoked it is given an array of strings called the
                environment. This is a list of name-value pairs, of the form
                name=value.

                The shell provides several ways to manipulate the environment. On
                invocation, the shell scans its own environment and creates a parameter
                for each name found, automatically marking it for export to child pro-
                cesses. Executed commands inherit the environment.


                In practical terms, it allows you to define behavior that is shared or unique to programs invoked from the present shell. For example, when using crontab or visudo you can define the EDITOR environment variable to define another editor other than the one your system would use by default. The same can be held true for things like the man command which looks at your PAGER environment to work out what pager program should be used to display the output of the man page with.



                Quite a lot of unix commands read the environment and depending on what is set there alter their output/processing/action depending on these. Some are shared, some are unique to the program. Most man pages contain information on how the environment variable have an effect on the described program.



                Other practical illustrations are for things such as systems with several installs of Oracle on the same platform. By setting ORACLE_HOME, the whole suite of oracle commands (as loaded from your PATH environment variable) then pull settings, definitions, mappings and libraries from under that top level directory. The same hold true for other programs such as java with it's JAVA_HOME environment variable.



                bash itself has many environment variables which can change the behavior of a range of things from history (HISTSIZE, HISTFILE etc), screen size (COLUMNS), tab completion (FIGNORE,GLOBIGNORE) locale and character encoding/decoding (LANG, LC_*), prompt (PS1 .. PS4), and so forth (again seek knowledge from the bash man page).



                Also you can write scripts/programs that make use of your own custom environment variables (to pass settings, or change functionality).







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Sep 18 '13 at 19:01

























                answered Sep 18 '13 at 18:51









                Drav SloanDrav Sloan

                10.1k23339




                10.1k23339





















                    0














                    "Environment Variables" are a set of dynamic named values that can affect the way running processes will behave on a computer.



                    They are part of the operating environment in which a process runs. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME or USERPROFILE variable to find the directory structure owned by the user running the process.



                    More info here &rightarrow; http://en.wikipedia.org/wiki/Environment_variable.



                    Everything you want to know about Environment Variables... &uparrow;






                    share|improve this answer




















                    • 1





                      Although it is unlikely that this links goes away, it is better to answer the question here with relevant text and provide the link as an addition for backup information.

                      – Anthon
                      Sep 19 '13 at 7:22











                    • @Anthon I do believe you are correct and I will make the changes as soon as I can... Thanks for the advise...

                      – SoCalDiegoRob
                      Sep 19 '13 at 8:37















                    0














                    "Environment Variables" are a set of dynamic named values that can affect the way running processes will behave on a computer.



                    They are part of the operating environment in which a process runs. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME or USERPROFILE variable to find the directory structure owned by the user running the process.



                    More info here &rightarrow; http://en.wikipedia.org/wiki/Environment_variable.



                    Everything you want to know about Environment Variables... &uparrow;






                    share|improve this answer




















                    • 1





                      Although it is unlikely that this links goes away, it is better to answer the question here with relevant text and provide the link as an addition for backup information.

                      – Anthon
                      Sep 19 '13 at 7:22











                    • @Anthon I do believe you are correct and I will make the changes as soon as I can... Thanks for the advise...

                      – SoCalDiegoRob
                      Sep 19 '13 at 8:37













                    0












                    0








                    0







                    "Environment Variables" are a set of dynamic named values that can affect the way running processes will behave on a computer.



                    They are part of the operating environment in which a process runs. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME or USERPROFILE variable to find the directory structure owned by the user running the process.



                    More info here &rightarrow; http://en.wikipedia.org/wiki/Environment_variable.



                    Everything you want to know about Environment Variables... &uparrow;






                    share|improve this answer















                    "Environment Variables" are a set of dynamic named values that can affect the way running processes will behave on a computer.



                    They are part of the operating environment in which a process runs. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME or USERPROFILE variable to find the directory structure owned by the user running the process.



                    More info here &rightarrow; http://en.wikipedia.org/wiki/Environment_variable.



                    Everything you want to know about Environment Variables... &uparrow;







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 10 hours ago









                    Rui F Ribeiro

                    42.1k1484142




                    42.1k1484142










                    answered Sep 19 '13 at 7:05









                    SoCalDiegoRobSoCalDiegoRob

                    1012




                    1012







                    • 1





                      Although it is unlikely that this links goes away, it is better to answer the question here with relevant text and provide the link as an addition for backup information.

                      – Anthon
                      Sep 19 '13 at 7:22











                    • @Anthon I do believe you are correct and I will make the changes as soon as I can... Thanks for the advise...

                      – SoCalDiegoRob
                      Sep 19 '13 at 8:37












                    • 1





                      Although it is unlikely that this links goes away, it is better to answer the question here with relevant text and provide the link as an addition for backup information.

                      – Anthon
                      Sep 19 '13 at 7:22











                    • @Anthon I do believe you are correct and I will make the changes as soon as I can... Thanks for the advise...

                      – SoCalDiegoRob
                      Sep 19 '13 at 8:37







                    1




                    1





                    Although it is unlikely that this links goes away, it is better to answer the question here with relevant text and provide the link as an addition for backup information.

                    – Anthon
                    Sep 19 '13 at 7:22





                    Although it is unlikely that this links goes away, it is better to answer the question here with relevant text and provide the link as an addition for backup information.

                    – Anthon
                    Sep 19 '13 at 7:22













                    @Anthon I do believe you are correct and I will make the changes as soon as I can... Thanks for the advise...

                    – SoCalDiegoRob
                    Sep 19 '13 at 8:37





                    @Anthon I do believe you are correct and I will make the changes as soon as I can... Thanks for the advise...

                    – SoCalDiegoRob
                    Sep 19 '13 at 8:37











                    -1














                    This answer requires some shell scripting experience and knowledge with the terms variable, value, variable substitution, prompt, echo, kernel, shell, utility, session and process.



                    An environment variable (envar) is a set of global defined variables that can effect the way a given processes will behave on a computer's operating system.



                    1. An exemplary introduction:



                    We substitute envars with a $ and capitalized letters. For example: $PS1.



                    We can print an envar this way:



                    echo $PS1


                    $PS1 holds the value of the Unix prompt. Say its native values are u w $.




                    • u stands for (current) user,


                    • w stands for working directory,


                    • $ is to border the prompt.

                    So, if we do: echo $PS1, we see the values of u, w plus the dollar sign in the end.



                    We could change the Unix behavior in that context, if we change the values of that envar. For example:



                    PS1="w >"


                    Now the prompt looks like this (assuming the work directory is named "John"):



                    John >


                    In the same manner we could do PS1="Hello, I'm your prompt >", so echo $PS1 will bring:



                    Hello, I'm your prompt >


                    In Bash 4.x.x, we can print ALL envars in the system with the env command. I suggest executing env in the terminal and take some look at the output.



                    2. How are these data shown and manipulated:



                    The terminal of a session let's us to customize the envars that are coming with Bash.



                    The aforementioned changes are usually temporary, and here's why:



                    Each session (which isn't a sub-session) is unique, and several processes can run uniquely at the same time (each with its own set of envars) but usually there is inheritance from session 0 to session 1 and upwards.



                    Changes we make to one process are unique to it, and will cease if we close it without saving them in some way.



                    So how can we save these changes:



                    There are several types of ways available to store envar changes, depending on the scope we pick. Here are different scopes (levels) for such changes:



                    • Process level: The envars are only available for programs in the current session.

                    • Export level: The envars are available for programs in the current session, or all its sub-sessions.

                    • Global level: The changes will be stored for all sessions whatsoever (primary and all subs).

                    Where are envar data stored:



                    Unix is built of 3 main layers: Kernel, shell, and utilities. AFAIK each shell has its own envars, and these are built primarily or exclusively in the shell.



                    The specific location in which to globally change these is usually /etc/profile though we can also do that in .bashrc of course.



                    3. Creating new envars:



                    We can create new envars and here is a way; as of Bash 4.x.x there is no native enavar named MESSAGE (as said, envars are usually uppercased).



                    MESSAGE="Hello world!"


                    will create it for us, and now if we type echo $MESSAGE, we get hello world!.



                    If we'll execute bash in our current working session (window), we would start a new bash sub-session and will no longer work in the original process, unless we execute exit.



                    Note: In operating systems with a terminal emulator (like Ubuntu desktop), a sub-session usually runs on the same window, but a new session in another window isn't a sub-session of the existing one (it's an adjacent process).



                    Note: Don't use special signs in envar values such as ! or they won't be saved.



                    Exporting the envar from the original session to all sub-sessions:



                    We can still use the envar created in the first session, in the second one as well, without registering it in the user or global level conf files (see following data). Here's how to do that:



                    Go to the original session (whether on the current window or another) and execute:



                    export MESSAGE


                    when exporting, don't use a $ sign.



                    It is now exported to all sub-sessions. If you'll do echo $MESSAGE on a sub-session, whether from your user or another, it will then be printed.



                    Note that Shell internal variables such as PS1 should not be exported, but if you do want to export them from whatever reason and they don't appear, don't execute bash after export, but rather bash –norc.



                    4. The $PATH envar:



                    $PATH is the envar that users will usually change the most.



                    If we echo $PATH, we are going to see this stream:



                    /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games


                    The printed values of this envar are separated by colons (:) there, but here's a potentially more comfortable way (these are the same values):



                    /usr/local/bin
                    /usr/bin
                    /bin
                    /usr/local/games
                    /usr/games


                    These are direcotries to search for, when we run a utility.



                    By executing which echo we will get its file location - for example, we might see it exists in /bin/echo.



                    Based on that we don't have to type echo envar to view the evnar's values. We can also do:



                    /bin/echo $ENVAR


                    The envar will still be executed, for example:



                    /bin/echo $HOME


                    Gives us



                    /home/User || /root


                    Just as:



                    echo $HOME


                    Gives us



                    /home/User || /root


                    Note: $HOME is abbreviated as ~.



                    The system-$PATH relations, and a possible user interaction:



                    In Bash 4.x.x, when we use a utility without its full path, the system will use all 6 values mentioned above, of the $PATH envar. So, it will start from /user/local/bin, and will follow all its content looking for the echo executable.



                    In this case, it will stop at /bin/echo, in which, in this case, the executable resides.



                    Hence, the main reason we might customize the $PATH envar, is installing executables that are not under any of its native values.



                    After installing such executables, we should set their $PATH value accordingly and then we would be able to work with them.



                    5. Appendix - expanding $PATH:



                    We can export $PATH to bash sub-sessions (that includes bash extensions like WP-CLI for WordPress or Drush for Drupal ) this way:



                    export PATH="/home/John:$PATH"


                    This will add a new value /home/John to $PATH, and then right afterwards, it will annex any native values to it (right after the colon), which are stored under the syntax $PATH.



                    Such permanent change can be done in the relevant script, usually under /etc/profile and by the name .bashrc.






                    share|improve this answer




















                    • 3





                      There is an awful lot wrong with this answer: conflation of sessions and processes, warning about ! in an environment variable value not working that is right below an example showing it working, a false notion of sub-sessions, quite bizarre advice about what to do after exporting a shell variable, and a false notion of global environment variables.

                      – JdeBP
                      Nov 1 '18 at 17:34











                    • warning about ! in an environment variable value not working that is right below an example showing it working? Please example.

                      – JohnDoea
                      Nov 1 '18 at 17:46











                    • quite bizarre advice about what to do after exporting a shell variable, what exactly do you mean?

                      – JohnDoea
                      Nov 1 '18 at 17:47












                    • false notion of global environment variables, what exactly do you mean?

                      – JohnDoea
                      Nov 1 '18 at 17:48















                    -1














                    This answer requires some shell scripting experience and knowledge with the terms variable, value, variable substitution, prompt, echo, kernel, shell, utility, session and process.



                    An environment variable (envar) is a set of global defined variables that can effect the way a given processes will behave on a computer's operating system.



                    1. An exemplary introduction:



                    We substitute envars with a $ and capitalized letters. For example: $PS1.



                    We can print an envar this way:



                    echo $PS1


                    $PS1 holds the value of the Unix prompt. Say its native values are u w $.




                    • u stands for (current) user,


                    • w stands for working directory,


                    • $ is to border the prompt.

                    So, if we do: echo $PS1, we see the values of u, w plus the dollar sign in the end.



                    We could change the Unix behavior in that context, if we change the values of that envar. For example:



                    PS1="w >"


                    Now the prompt looks like this (assuming the work directory is named "John"):



                    John >


                    In the same manner we could do PS1="Hello, I'm your prompt >", so echo $PS1 will bring:



                    Hello, I'm your prompt >


                    In Bash 4.x.x, we can print ALL envars in the system with the env command. I suggest executing env in the terminal and take some look at the output.



                    2. How are these data shown and manipulated:



                    The terminal of a session let's us to customize the envars that are coming with Bash.



                    The aforementioned changes are usually temporary, and here's why:



                    Each session (which isn't a sub-session) is unique, and several processes can run uniquely at the same time (each with its own set of envars) but usually there is inheritance from session 0 to session 1 and upwards.



                    Changes we make to one process are unique to it, and will cease if we close it without saving them in some way.



                    So how can we save these changes:



                    There are several types of ways available to store envar changes, depending on the scope we pick. Here are different scopes (levels) for such changes:



                    • Process level: The envars are only available for programs in the current session.

                    • Export level: The envars are available for programs in the current session, or all its sub-sessions.

                    • Global level: The changes will be stored for all sessions whatsoever (primary and all subs).

                    Where are envar data stored:



                    Unix is built of 3 main layers: Kernel, shell, and utilities. AFAIK each shell has its own envars, and these are built primarily or exclusively in the shell.



                    The specific location in which to globally change these is usually /etc/profile though we can also do that in .bashrc of course.



                    3. Creating new envars:



                    We can create new envars and here is a way; as of Bash 4.x.x there is no native enavar named MESSAGE (as said, envars are usually uppercased).



                    MESSAGE="Hello world!"


                    will create it for us, and now if we type echo $MESSAGE, we get hello world!.



                    If we'll execute bash in our current working session (window), we would start a new bash sub-session and will no longer work in the original process, unless we execute exit.



                    Note: In operating systems with a terminal emulator (like Ubuntu desktop), a sub-session usually runs on the same window, but a new session in another window isn't a sub-session of the existing one (it's an adjacent process).



                    Note: Don't use special signs in envar values such as ! or they won't be saved.



                    Exporting the envar from the original session to all sub-sessions:



                    We can still use the envar created in the first session, in the second one as well, without registering it in the user or global level conf files (see following data). Here's how to do that:



                    Go to the original session (whether on the current window or another) and execute:



                    export MESSAGE


                    when exporting, don't use a $ sign.



                    It is now exported to all sub-sessions. If you'll do echo $MESSAGE on a sub-session, whether from your user or another, it will then be printed.



                    Note that Shell internal variables such as PS1 should not be exported, but if you do want to export them from whatever reason and they don't appear, don't execute bash after export, but rather bash –norc.



                    4. The $PATH envar:



                    $PATH is the envar that users will usually change the most.



                    If we echo $PATH, we are going to see this stream:



                    /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games


                    The printed values of this envar are separated by colons (:) there, but here's a potentially more comfortable way (these are the same values):



                    /usr/local/bin
                    /usr/bin
                    /bin
                    /usr/local/games
                    /usr/games


                    These are direcotries to search for, when we run a utility.



                    By executing which echo we will get its file location - for example, we might see it exists in /bin/echo.



                    Based on that we don't have to type echo envar to view the evnar's values. We can also do:



                    /bin/echo $ENVAR


                    The envar will still be executed, for example:



                    /bin/echo $HOME


                    Gives us



                    /home/User || /root


                    Just as:



                    echo $HOME


                    Gives us



                    /home/User || /root


                    Note: $HOME is abbreviated as ~.



                    The system-$PATH relations, and a possible user interaction:



                    In Bash 4.x.x, when we use a utility without its full path, the system will use all 6 values mentioned above, of the $PATH envar. So, it will start from /user/local/bin, and will follow all its content looking for the echo executable.



                    In this case, it will stop at /bin/echo, in which, in this case, the executable resides.



                    Hence, the main reason we might customize the $PATH envar, is installing executables that are not under any of its native values.



                    After installing such executables, we should set their $PATH value accordingly and then we would be able to work with them.



                    5. Appendix - expanding $PATH:



                    We can export $PATH to bash sub-sessions (that includes bash extensions like WP-CLI for WordPress or Drush for Drupal ) this way:



                    export PATH="/home/John:$PATH"


                    This will add a new value /home/John to $PATH, and then right afterwards, it will annex any native values to it (right after the colon), which are stored under the syntax $PATH.



                    Such permanent change can be done in the relevant script, usually under /etc/profile and by the name .bashrc.






                    share|improve this answer




















                    • 3





                      There is an awful lot wrong with this answer: conflation of sessions and processes, warning about ! in an environment variable value not working that is right below an example showing it working, a false notion of sub-sessions, quite bizarre advice about what to do after exporting a shell variable, and a false notion of global environment variables.

                      – JdeBP
                      Nov 1 '18 at 17:34











                    • warning about ! in an environment variable value not working that is right below an example showing it working? Please example.

                      – JohnDoea
                      Nov 1 '18 at 17:46











                    • quite bizarre advice about what to do after exporting a shell variable, what exactly do you mean?

                      – JohnDoea
                      Nov 1 '18 at 17:47












                    • false notion of global environment variables, what exactly do you mean?

                      – JohnDoea
                      Nov 1 '18 at 17:48













                    -1












                    -1








                    -1







                    This answer requires some shell scripting experience and knowledge with the terms variable, value, variable substitution, prompt, echo, kernel, shell, utility, session and process.



                    An environment variable (envar) is a set of global defined variables that can effect the way a given processes will behave on a computer's operating system.



                    1. An exemplary introduction:



                    We substitute envars with a $ and capitalized letters. For example: $PS1.



                    We can print an envar this way:



                    echo $PS1


                    $PS1 holds the value of the Unix prompt. Say its native values are u w $.




                    • u stands for (current) user,


                    • w stands for working directory,


                    • $ is to border the prompt.

                    So, if we do: echo $PS1, we see the values of u, w plus the dollar sign in the end.



                    We could change the Unix behavior in that context, if we change the values of that envar. For example:



                    PS1="w >"


                    Now the prompt looks like this (assuming the work directory is named "John"):



                    John >


                    In the same manner we could do PS1="Hello, I'm your prompt >", so echo $PS1 will bring:



                    Hello, I'm your prompt >


                    In Bash 4.x.x, we can print ALL envars in the system with the env command. I suggest executing env in the terminal and take some look at the output.



                    2. How are these data shown and manipulated:



                    The terminal of a session let's us to customize the envars that are coming with Bash.



                    The aforementioned changes are usually temporary, and here's why:



                    Each session (which isn't a sub-session) is unique, and several processes can run uniquely at the same time (each with its own set of envars) but usually there is inheritance from session 0 to session 1 and upwards.



                    Changes we make to one process are unique to it, and will cease if we close it without saving them in some way.



                    So how can we save these changes:



                    There are several types of ways available to store envar changes, depending on the scope we pick. Here are different scopes (levels) for such changes:



                    • Process level: The envars are only available for programs in the current session.

                    • Export level: The envars are available for programs in the current session, or all its sub-sessions.

                    • Global level: The changes will be stored for all sessions whatsoever (primary and all subs).

                    Where are envar data stored:



                    Unix is built of 3 main layers: Kernel, shell, and utilities. AFAIK each shell has its own envars, and these are built primarily or exclusively in the shell.



                    The specific location in which to globally change these is usually /etc/profile though we can also do that in .bashrc of course.



                    3. Creating new envars:



                    We can create new envars and here is a way; as of Bash 4.x.x there is no native enavar named MESSAGE (as said, envars are usually uppercased).



                    MESSAGE="Hello world!"


                    will create it for us, and now if we type echo $MESSAGE, we get hello world!.



                    If we'll execute bash in our current working session (window), we would start a new bash sub-session and will no longer work in the original process, unless we execute exit.



                    Note: In operating systems with a terminal emulator (like Ubuntu desktop), a sub-session usually runs on the same window, but a new session in another window isn't a sub-session of the existing one (it's an adjacent process).



                    Note: Don't use special signs in envar values such as ! or they won't be saved.



                    Exporting the envar from the original session to all sub-sessions:



                    We can still use the envar created in the first session, in the second one as well, without registering it in the user or global level conf files (see following data). Here's how to do that:



                    Go to the original session (whether on the current window or another) and execute:



                    export MESSAGE


                    when exporting, don't use a $ sign.



                    It is now exported to all sub-sessions. If you'll do echo $MESSAGE on a sub-session, whether from your user or another, it will then be printed.



                    Note that Shell internal variables such as PS1 should not be exported, but if you do want to export them from whatever reason and they don't appear, don't execute bash after export, but rather bash –norc.



                    4. The $PATH envar:



                    $PATH is the envar that users will usually change the most.



                    If we echo $PATH, we are going to see this stream:



                    /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games


                    The printed values of this envar are separated by colons (:) there, but here's a potentially more comfortable way (these are the same values):



                    /usr/local/bin
                    /usr/bin
                    /bin
                    /usr/local/games
                    /usr/games


                    These are direcotries to search for, when we run a utility.



                    By executing which echo we will get its file location - for example, we might see it exists in /bin/echo.



                    Based on that we don't have to type echo envar to view the evnar's values. We can also do:



                    /bin/echo $ENVAR


                    The envar will still be executed, for example:



                    /bin/echo $HOME


                    Gives us



                    /home/User || /root


                    Just as:



                    echo $HOME


                    Gives us



                    /home/User || /root


                    Note: $HOME is abbreviated as ~.



                    The system-$PATH relations, and a possible user interaction:



                    In Bash 4.x.x, when we use a utility without its full path, the system will use all 6 values mentioned above, of the $PATH envar. So, it will start from /user/local/bin, and will follow all its content looking for the echo executable.



                    In this case, it will stop at /bin/echo, in which, in this case, the executable resides.



                    Hence, the main reason we might customize the $PATH envar, is installing executables that are not under any of its native values.



                    After installing such executables, we should set their $PATH value accordingly and then we would be able to work with them.



                    5. Appendix - expanding $PATH:



                    We can export $PATH to bash sub-sessions (that includes bash extensions like WP-CLI for WordPress or Drush for Drupal ) this way:



                    export PATH="/home/John:$PATH"


                    This will add a new value /home/John to $PATH, and then right afterwards, it will annex any native values to it (right after the colon), which are stored under the syntax $PATH.



                    Such permanent change can be done in the relevant script, usually under /etc/profile and by the name .bashrc.






                    share|improve this answer















                    This answer requires some shell scripting experience and knowledge with the terms variable, value, variable substitution, prompt, echo, kernel, shell, utility, session and process.



                    An environment variable (envar) is a set of global defined variables that can effect the way a given processes will behave on a computer's operating system.



                    1. An exemplary introduction:



                    We substitute envars with a $ and capitalized letters. For example: $PS1.



                    We can print an envar this way:



                    echo $PS1


                    $PS1 holds the value of the Unix prompt. Say its native values are u w $.




                    • u stands for (current) user,


                    • w stands for working directory,


                    • $ is to border the prompt.

                    So, if we do: echo $PS1, we see the values of u, w plus the dollar sign in the end.



                    We could change the Unix behavior in that context, if we change the values of that envar. For example:



                    PS1="w >"


                    Now the prompt looks like this (assuming the work directory is named "John"):



                    John >


                    In the same manner we could do PS1="Hello, I'm your prompt >", so echo $PS1 will bring:



                    Hello, I'm your prompt >


                    In Bash 4.x.x, we can print ALL envars in the system with the env command. I suggest executing env in the terminal and take some look at the output.



                    2. How are these data shown and manipulated:



                    The terminal of a session let's us to customize the envars that are coming with Bash.



                    The aforementioned changes are usually temporary, and here's why:



                    Each session (which isn't a sub-session) is unique, and several processes can run uniquely at the same time (each with its own set of envars) but usually there is inheritance from session 0 to session 1 and upwards.



                    Changes we make to one process are unique to it, and will cease if we close it without saving them in some way.



                    So how can we save these changes:



                    There are several types of ways available to store envar changes, depending on the scope we pick. Here are different scopes (levels) for such changes:



                    • Process level: The envars are only available for programs in the current session.

                    • Export level: The envars are available for programs in the current session, or all its sub-sessions.

                    • Global level: The changes will be stored for all sessions whatsoever (primary and all subs).

                    Where are envar data stored:



                    Unix is built of 3 main layers: Kernel, shell, and utilities. AFAIK each shell has its own envars, and these are built primarily or exclusively in the shell.



                    The specific location in which to globally change these is usually /etc/profile though we can also do that in .bashrc of course.



                    3. Creating new envars:



                    We can create new envars and here is a way; as of Bash 4.x.x there is no native enavar named MESSAGE (as said, envars are usually uppercased).



                    MESSAGE="Hello world!"


                    will create it for us, and now if we type echo $MESSAGE, we get hello world!.



                    If we'll execute bash in our current working session (window), we would start a new bash sub-session and will no longer work in the original process, unless we execute exit.



                    Note: In operating systems with a terminal emulator (like Ubuntu desktop), a sub-session usually runs on the same window, but a new session in another window isn't a sub-session of the existing one (it's an adjacent process).



                    Note: Don't use special signs in envar values such as ! or they won't be saved.



                    Exporting the envar from the original session to all sub-sessions:



                    We can still use the envar created in the first session, in the second one as well, without registering it in the user or global level conf files (see following data). Here's how to do that:



                    Go to the original session (whether on the current window or another) and execute:



                    export MESSAGE


                    when exporting, don't use a $ sign.



                    It is now exported to all sub-sessions. If you'll do echo $MESSAGE on a sub-session, whether from your user or another, it will then be printed.



                    Note that Shell internal variables such as PS1 should not be exported, but if you do want to export them from whatever reason and they don't appear, don't execute bash after export, but rather bash –norc.



                    4. The $PATH envar:



                    $PATH is the envar that users will usually change the most.



                    If we echo $PATH, we are going to see this stream:



                    /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games


                    The printed values of this envar are separated by colons (:) there, but here's a potentially more comfortable way (these are the same values):



                    /usr/local/bin
                    /usr/bin
                    /bin
                    /usr/local/games
                    /usr/games


                    These are direcotries to search for, when we run a utility.



                    By executing which echo we will get its file location - for example, we might see it exists in /bin/echo.



                    Based on that we don't have to type echo envar to view the evnar's values. We can also do:



                    /bin/echo $ENVAR


                    The envar will still be executed, for example:



                    /bin/echo $HOME


                    Gives us



                    /home/User || /root


                    Just as:



                    echo $HOME


                    Gives us



                    /home/User || /root


                    Note: $HOME is abbreviated as ~.



                    The system-$PATH relations, and a possible user interaction:



                    In Bash 4.x.x, when we use a utility without its full path, the system will use all 6 values mentioned above, of the $PATH envar. So, it will start from /user/local/bin, and will follow all its content looking for the echo executable.



                    In this case, it will stop at /bin/echo, in which, in this case, the executable resides.



                    Hence, the main reason we might customize the $PATH envar, is installing executables that are not under any of its native values.



                    After installing such executables, we should set their $PATH value accordingly and then we would be able to work with them.



                    5. Appendix - expanding $PATH:



                    We can export $PATH to bash sub-sessions (that includes bash extensions like WP-CLI for WordPress or Drush for Drupal ) this way:



                    export PATH="/home/John:$PATH"


                    This will add a new value /home/John to $PATH, and then right afterwards, it will annex any native values to it (right after the colon), which are stored under the syntax $PATH.



                    Such permanent change can be done in the relevant script, usually under /etc/profile and by the name .bashrc.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Oct 27 '18 at 14:39

























                    answered Oct 26 '18 at 19:24









                    JohnDoeaJohnDoea

                    6911138




                    6911138







                    • 3





                      There is an awful lot wrong with this answer: conflation of sessions and processes, warning about ! in an environment variable value not working that is right below an example showing it working, a false notion of sub-sessions, quite bizarre advice about what to do after exporting a shell variable, and a false notion of global environment variables.

                      – JdeBP
                      Nov 1 '18 at 17:34











                    • warning about ! in an environment variable value not working that is right below an example showing it working? Please example.

                      – JohnDoea
                      Nov 1 '18 at 17:46











                    • quite bizarre advice about what to do after exporting a shell variable, what exactly do you mean?

                      – JohnDoea
                      Nov 1 '18 at 17:47












                    • false notion of global environment variables, what exactly do you mean?

                      – JohnDoea
                      Nov 1 '18 at 17:48












                    • 3





                      There is an awful lot wrong with this answer: conflation of sessions and processes, warning about ! in an environment variable value not working that is right below an example showing it working, a false notion of sub-sessions, quite bizarre advice about what to do after exporting a shell variable, and a false notion of global environment variables.

                      – JdeBP
                      Nov 1 '18 at 17:34











                    • warning about ! in an environment variable value not working that is right below an example showing it working? Please example.

                      – JohnDoea
                      Nov 1 '18 at 17:46











                    • quite bizarre advice about what to do after exporting a shell variable, what exactly do you mean?

                      – JohnDoea
                      Nov 1 '18 at 17:47












                    • false notion of global environment variables, what exactly do you mean?

                      – JohnDoea
                      Nov 1 '18 at 17:48







                    3




                    3





                    There is an awful lot wrong with this answer: conflation of sessions and processes, warning about ! in an environment variable value not working that is right below an example showing it working, a false notion of sub-sessions, quite bizarre advice about what to do after exporting a shell variable, and a false notion of global environment variables.

                    – JdeBP
                    Nov 1 '18 at 17:34





                    There is an awful lot wrong with this answer: conflation of sessions and processes, warning about ! in an environment variable value not working that is right below an example showing it working, a false notion of sub-sessions, quite bizarre advice about what to do after exporting a shell variable, and a false notion of global environment variables.

                    – JdeBP
                    Nov 1 '18 at 17:34













                    warning about ! in an environment variable value not working that is right below an example showing it working? Please example.

                    – JohnDoea
                    Nov 1 '18 at 17:46





                    warning about ! in an environment variable value not working that is right below an example showing it working? Please example.

                    – JohnDoea
                    Nov 1 '18 at 17:46













                    quite bizarre advice about what to do after exporting a shell variable, what exactly do you mean?

                    – JohnDoea
                    Nov 1 '18 at 17:47






                    quite bizarre advice about what to do after exporting a shell variable, what exactly do you mean?

                    – JohnDoea
                    Nov 1 '18 at 17:47














                    false notion of global environment variables, what exactly do you mean?

                    – JohnDoea
                    Nov 1 '18 at 17:48





                    false notion of global environment variables, what exactly do you mean?

                    – JohnDoea
                    Nov 1 '18 at 17:48

















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f91282%2fwhat-exactly-is-an-environment-variable%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    -bash, environment-variables, shell

                    Popular posts from this blog

                    Frič See also Navigation menuinternal link

                    Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant

                    fontconfig warning: “/etc/fonts/fonts.conf”, line 100: unknown “element blank” The 2019 Stack Overflow Developer Survey Results Are In“tar: unrecognized option --warning” during 'apt-get install'How to fix Fontconfig errorHow do I figure out which font file is chosen for a system generic font alias?Why are some apt-get-installed fonts being ignored by fc-list, xfontsel, etc?Reload settings in /etc/fonts/conf.dTaking 30 seconds longer to boot after upgrade from jessie to stretchHow to match multiple font names with a single <match> element?Adding a custom font to fontconfigRemoving fonts from fontconfig <match> resultsBroken fonts after upgrading Firefox ESR to latest Firefox