Skip to content
Advertisement

local variables in linux

When we export a local variable declared within a current shell does it get passed to future sub shells,processes , child processes or future child processes? I was told it get passed to future sub shells. Is it correct?

Advertisement

Answer

This simple test will answer you by itself:

$ VAR1="Hello, World!"
$ echo "${VAR1}"
Hello, World!
$ bash
$ echo "${VAR1}"

$ export VAR1="Hello, World!"
$ echo "${VAR1}"
Hello, World!
$ bash
$ echo "${VAR1}"
Hello, World!
$ 

Breaking it down:

No export …

$ VAR1="Hello, World!"
$ echo "${VAR1}"
Hello, World!
$ bash
$ echo "${VAR1}"

$

… leads to VAR1 not being defined in the child.

While with export

$ export VAR1="Hello, World!"
$ echo "${VAR1}"
Hello, World!
$ bash
$ echo "${VAR1}"
Hello, World!
$ 

… leads to VAR1 being defined in the child.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement