3

Do you need to include export for environment variables in Bash profile / zshrc?

I'm using Z shell (Zsh) for my terminal and in my .zshrc file I have the two lines:

varOne="foo"

export varTwo="bar"

When I echo either variable (ex: echo $varOne) within the terminal, the correct value is output.

So is there a difference when prefixing the environment variable declaration with export inside the .zshrc file?

informatik01
  • 16,038
  • 10
  • 74
  • 104
Domenick
  • 2,142
  • 3
  • 12
  • 23
  • 2
    If you do not `export` variable, it's only available inside the `.zshrc` file. Also maybe see related Q&A: https://stackoverflow.com/a/1158093/4676641 – cam Jan 04 '22 at 22:05
  • 2
    the first one is just for assigning a variable and the second one (with export), sends the variable to child shell proceses created from that shell. https://www.baeldung.com/linux/bash-variables-export – Omar Alvarado Jan 04 '22 at 22:06
  • 2
    @tentative No, it's available in the shell that sources `.zshrc`. The shell doesn't have file-local variables, because the shell has no concept of modules. – chepner Jan 06 '22 at 15:35

1 Answers1

7

So is there a difference when prefixing the environment variable declaration with export inside the .zshrc file?

Yes, one is an environment variable and the other isn't.

The difference doesn't matter (much) to your shell, but to processes started by your shell. Environment variables are inherited by child processes, regular shell variables are not.

~ % foo=3; printenv foo
~ % export foo=3; printenv foo
3

In the first case, printenv has no variable named foo in its environment; in the second case, it does.

chepner
  • 497,756
  • 71
  • 530
  • 681