When we set a environment variable in bash we have to export it. so in future we can use it.(atleast in a continuous session). I understand this.

My question is what exactly doe the export command does to the variable?

Does it create a temp file somewhere to use it, or it modify some file to save the variable? This is quite confusing.

So basically when we work in shell it is backed up by a environment to process all command and other things and all variables are part of it. When we export a newly created variable in shell, that variable also join the environment.? And in this process not any file gets modified or store that newly variable anywhere.?


No, you don't have to export a shell variable to be able to use it in the same shell session. You export a variable when it is needed in child process, such as in a new shell or if it's needed by some tool that you start from the current shell.

Exporting a shell variable makes it an environment variable. Environment variables are part of the environment. Other things that are also part of the environment are things like the current working directory and the current process' "niceness" value, and other bits and pieces.

The environment is inherited, copied, when a new process is started. A new process would therefore get a copy of all environment variables from the shell that started it, but not a copy of shell variables (un-exported variables).

This does not involve any temporary files, just copies of data in memory. The copying is done by the kernel when a new process is started.

  • 2
    You don't need to export a variable for child processes. Children inherit all the data of their parent. You need to export for that shell variable to be passed as an environment variable to another command, across execution (most the data of a process is wiped when it executes a command) whether it's in the same process or a child process. – Stéphane Chazelas May 2 '20 at 16:15
  • @StéphaneChazelas How do you define "child process" there? Because it sounds like what you call "another command" is what I call a "child process". A child process is a process started by executing a command in the shell. That command runs as a separate process, a child process. I use "child process" as distinct from "subshell". If I could get input on how to correct the terminology, I'd be happy. – Kusalananda May 2 '20 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.