Defining a variable with or without export

后端 未结 14 1465
借酒劲吻你
借酒劲吻你 2020-11-22 09:58

What is export for?

What is the difference between:

export name=value

and

name=value
相关标签:
14条回答
  • 2020-11-22 10:44

    Two of the creators of UNIX, Brian Kernighan and Rob Pike, explain this in their book "The UNIX Programming Environment". Google for the title and you'll easily find a pdf version.

    They address shell variables in section 3.6, and focus on the use of the export command at the end of that section:

    When you want to make the value of a variable accessible in sub-shells, the shell's export command should be used. (You might think about why there is no way to export the value of a variable from a sub-shell to its parent).

    0 讨论(0)
  • 2020-11-22 10:51

    It has been said that it's not necessary to export in bash when spawning subshells, while others said the exact opposite. It is important to note the difference between subshells (those that are created by (), ``, $() or loops) and subprocesses (processes that are invoked by name, for example a literal bash appearing in your script).

    • Subshells will have access to all variables from the parent, regardless of their exported state.
    • Subprocesses will only see the exported variables.

    What is common in these two constructs is that neither can pass variables back to the parent shell.

    $ noexport=noexport; export export=export; (echo subshell: $noexport $export; subshell=subshell); bash -c 'echo subprocess: $noexport $export; subprocess=subprocess'; echo parent: $subshell $subprocess
    subshell: noexport export
    subprocess: export
    parent:
    

    There is one more source of confusion: some think that 'forked' subprocesses are the ones that don't see non-exported variables. Usually fork()s are immediately followed by exec()s, and that's why it would seem that the fork() is the thing to look for, while in fact it's the exec(). You can run commands without fork()ing first with the exec command, and processes started by this method will also have no access to unexported variables:

    $ noexport=noexport; export export=export; exec bash -c 'echo execd process: $noexport $export; execd=execd'; echo parent: $execd
    execd process: export
    

    Note that we don't see the parent: line this time, because we have replaced the parent shell with the exec command, so there's nothing left to execute that command.

    0 讨论(0)
  • 2020-11-22 10:51

    Just to show the difference between an exported variable being in the environment (env) and a non-exported variable not being in the environment:

    If I do this:

    $ MYNAME=Fred
    $ export OURNAME=Jim
    

    then only $OURNAME appears in the env. The variable $MYNAME is not in the env.

    $ env | grep NAME
    OURNAME=Jim
    

    but the variable $MYNAME does exist in the shell

    $ echo $MYNAME
    Fred
    
    0 讨论(0)
  • 2020-11-22 10:55

    As you might already know, UNIX allows processes to have a set of environment variables, which are key/value pairs, both key and value being strings. Operating system is responsible for keeping these pairs for each process separately.

    Program can access its environment variables through this UNIX API:

    • char *getenv(const char *name);
    • int setenv(const char *name, const char *value, int override);
    • int unsetenv(const char *name);

    Processes also inherit environment variables from parent processes. Operating system is responsible for creating a copy of all "envars" at the moment the child process is created.

    Bash, among other shells, is capable of setting its environment variables on user request. This is what export exists for.

    export is a Bash command to set environment variable for Bash. All variables set with this command would be inherited by all processes that this Bash would create.

    More on Environment in Bash

    Another kind of variable in Bash is internal variable. Since Bash is not just interactive shell, it is in fact a script interpreter, as any other interpreter (e.g. Python) it is capable of keeping its own set of variables. It should be mentioned that Bash (unlike Python) supports only string variables.

    Notation for defining Bash variables is name=value. These variables stay inside Bash and have nothing to do with environment variables kept by operating system.

    More on Shell Parameters (including variables)

    Also worth noting that, according to Bash reference manual:

    The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters. These assignment statements affect only the environment seen by that command.


    To sum things up:

    • export is used to set environment variable in operating system. This variable will be available to all child processes created by current Bash process ever after.
    • Bash variable notation (name=value) is used to set local variables available only to current process of bash
    • Bash variable notation prefixing another command creates environment variable only for scope of that command.
    0 讨论(0)
  • 2020-11-22 10:56

    By default, variables created within a script are only available to the current shell; child processes (sub-shells) will not have access to values that have been set or modified. Allowing child processes to see the values, requires use of the export command.

    0 讨论(0)
  • 2020-11-22 10:57

    Others have answered that export makes the variable available to subshells, and that is correct but merely a side effect. When you export a variable, it puts that variable in the environment of the current shell (ie the shell calls putenv(3) or setenv(3)).
    The environment of a process is inherited across exec, making the variable visible in subshells.

    Edit (with 5 year's perspective): this is a silly answer. The purpose of 'export' is to make variables "be in the environment of subsequently executed commands", whether those commands be subshells or subprocesses. A naive implementation would be to simply put the variable in the environment of the shell, but this would make it impossible to implement export -p.

    0 讨论(0)
提交回复
热议问题