Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there a way to set environment variables from within a (bash) shell script?

I want to set some environment variables on some servers using a small script rather than typing. Some forum posts believe it is impossible.

Any Ideas?

share|improve this question
    
see also: superuser.com/questions/176783/… –  lesmana May 1 '11 at 12:46
add comment

3 Answers

up vote 5 down vote accepted

file export_FOOBAR:

# set variable FOOBAR to "hi"
FOOBAR=hi
export FOOBAR

at the prompt

yourhost:/~ > source export_FOOBAR
share|improve this answer
1  
Great. What dose 'source' mean? –  Adam Matan Aug 9 '09 at 12:13
1  
type "man source" in your shell –  Nifle Aug 9 '09 at 12:45
    
type "man <any_shell_command>" to find out what it does –  Nifle Aug 9 '09 at 12:47
    
I have none. It's probably part of bash - where can I find its docs? –  Adam Matan Aug 9 '09 at 12:48
1  
Good, but I wouldn't put #!/bin/bash (I assume that's what you meant) at the start of the script, as this script won't work if executed. –  Douglas Leeder Aug 9 '09 at 13:23
show 4 more comments

For Bash built-ins, use the built-in `help'. E.g.,

$ help source

To see if a command will be handled by the shell (is a built-in), use the `type' bash built-in:

$ type help

$ type type

share|improve this answer
add comment

or you could just execute: export $VARIABLE="stufffff"

share|improve this answer
    
It does not work from within a shell (tried it now again to be sure). –  Adam Matan Aug 9 '09 at 12:41
1  
Drop the $ in the assignment: export VARIABLE="stufffff" –  Richard Hoskins Aug 9 '09 at 15:16
    
But the question is how to do it in a script, so this is all moot. –  Richard Hoskins Aug 9 '09 at 15:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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