Why ‘export’ if i could just direct assign the variables in bash?

January 25th, 2011 mysurface Posted in Bash, export | Hits: 243120 | 15 Comments »

I can direct assign value to a variable in my bash shell why I still need the command ‘export’ ? What is the different between them?

If you have doubts about this, please continue to read on it.

Lets do an experiment in a bash shell:

$ HELLO=world
$ export HELLO2=world2
$ echo $HELLO $HELLO2
world world2

Hey, it works exactly the same!

Not really exactly the same if you use it in your bash script. Let me show you an example. Let say I have a bash script main.sh , which it calls child.sh.




export HELLO2=world2

Now lets execute ./main.sh

$ ./main.sh

Hey! the ‘world’ has gone!

When we assign the variable directly, we make the variable local, when we call a child script, the local variable will not available for the child script. In contrast, if we export the variable, we make it appear as an environment variable, which it will be available globally within that bash session. That is the reason, child.sh reads $HELLO2 but not $HELLO.

15 Responses to “Why ‘export’ if i could just direct assign the variables in bash?”

  1. Really good…

    nice example….

    Thanks a lot

  2. That was a good example and explanation! :)

  3. just cant understand linuxxx so complicated

  4. Hey thanks for this post.

    I was wondering if when you use export does the exported variable stays global forever or it is lost after closing the terminal?


  5. andreson: variables are never passed “out” of shells, so yes it is lost if you close the terminal (ie. exit your shell process). You can see the process more clearly like this: start a terminal, then do:

    $ export HELLO=world
    $ bash
    # this starts a sub-shell, just as if you’d entered a script
    $ echo $HELLO
    $ HELLO=goodbye
    $ echo $HELLO
    # now we exit the sub-shell
    $ exit
    # back in the main shell
    $ echo $HELLO

    exiting this shell again, and $HELLO is lost.

  6. Really nice example. helped a lot..

  7. Oh my goodness! Incredible article dude! Thank you so
    much, However I am going through troubles with your RSS. I don’t know why I cannot subscribe to it. Is there anyone else getting the same RSS problems? Anyone that knows the solution can you kindly respond? Thanx!!

  8. Because of this it is also one of the most expensive so
    getting a rebate helps keep the cost down.
    That tooth brush is an “Oral B Pulsar Soft” toothbrush.

    Long life rechargeable batteries – A properly charged battery can serve for a whole month without requiring further charging.

  9. Why ‘export’ if i could just direct assign the variables in bash? » Linux by Examples Hermes Voyage Pure Perfume

  10. Well. One comment:
    Its (almost) always good to weakly quote (double quote) variables.

    HELLO=”Hello world”
    echo “$Hello” # in `echo’ command it not a big deal.

    Else the spaces in the arguments may be misinterpreted in some commands.

  11. Astonishingly individual pleasant site. Astounding details offered on couple of clicks on

  12. say thanks to so mucha lot for your website it aids a whole lot

  13. Hi, just w?nted too say, I loved tthis blog post. ?t ??? helpful.

    Kee? on posting!

    Feel freee to visit my website précoce que faire

  14. Dalaman Göcek Transfer veya Göcek Transfer en güvenli seyehat imkan? sunuyoruz. Dalaman Havaliman? Göcek Transfer için hemen bizi aray?n. Göcek Dalaman Transfer ile ekonomik ula??m sizi bekliyor. http://www.dalamangocektransfer.com/

  15. Dalaman Havaliman? Transfer ile ekonomik fiyatlara seyahat edebilirsiniz. Dalaman Transfer için web sitemizi ziyaret edebilirsiniz. Dalaman Havaalan? Transfer ile Dalaman Marmaris Transfer, Dalaman Fethiye Transfer, Dalaman Göcek Transfer, Dalaman Ölüdeniz Transfer, Dalaman Kalkan Transfer, Dalaman Ka? Transfer, Dalaman Datça Transfer, Dalaman Dalyan Transfer ve Dalaman içmeler Transfer en popüler hizmetlerdir. https://kayatransfer.net/

Leave a Reply