Set environment variables in Bourne shell (sh)


An environment variable must be set and exported with separate commands in the Bourne shell. To set the EDITOR variable to vi, use:

EDITOR=vi
export EDITOR

These commands can be placed in ~/.profile to be executed automatically at login, placed in a shell script, or executed manually. Exporting a variable makes it available to other shells spawned from the current shell as when running another command or shell script.

To add to an existing variable, such as path, use this format:

PATH=$PATH:/usr/local/bin
export PATH EDITOR

Multiple variables can be exported with one export command.

 

About Quinn McHenry

Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
View more articles by Quinn McHenry

The Conversation

Follow the reactions below and share your own thoughts.

3 Responses to “Set environment variables in Bourne shell (sh)”

  1. March 29, 2010 at 10:39 am, Name said:

    Not right exactly.
    SH = Bourne Shell
    Bash = Bourne Again Shell
    http://en.wikipedia.org/wiki/Bourne_shell

    Reply

  2. May 03, 2010 at 5:25 am, Anonymous said:

    Can we do export and set in one command in bash ?

    Reply

  3. December 13, 2010 at 7:22 pm, Duncan Anderson said:

    As far as I know, the commands must be on separate lines in the Bourne shell. I n the Korn shell it is possible to use one line thus:

    EDITOR=vi export EDITOR

    If I am not mistaken this also works in Bash.

    Reply

Leave a Reply