Search code examples
bashenvironment-variableszshoh-my-zshzshrc

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


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?


Solution

  • 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.