Set environment variables from within a bash shell

1

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?

Adam Matan

Posted 2009-08-09T11:37:06.473

Reputation: 5 930

Answers

5

file export_FOOBAR:

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

at the prompt

yourhost:/~ > source export_FOOBAR

lexu

Posted 2009-08-09T11:37:06.473

Reputation: 1 822

BTW: If "man <command>" does not work, b/c the command is built into bash ,you can use "help <command>" . That invokes bash's internal help :-). – sleske – 2010-02-11T22:53:43.987

1Great. What dose 'source' mean? – Adam Matan – 2009-08-09T12:13:13.330

2type "man source" in your shell – Nifle – 2009-08-09T12:45:48.590

type "man <any_shell_command>" to find out what it does – Nifle – 2009-08-09T12:47:50.903

I have none. It's probably part of bash - where can I find its docs? – Adam Matan – 2009-08-09T12:48:35.647

type man bash and then look for the source command (within the bash man page search for 'SHELL BUILTIN COMMANDS' and it is usually shortly after that) – mas – 2009-08-09T12:57:36.387

Bash Reference Manual: http://www.gnu.org/software/bash/manual/bashref.html (your really need to learn about google :-)

– lexu – 2009-08-09T13:20:45.033

1Good, 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 – 2009-08-09T13:23:29.760

@Douglas Leeder : working from memory => errors, thx for the feedback – lexu – 2009-08-09T13:27:22.817

1

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

Iconoclastes

Posted 2009-08-09T11:37:06.473

Reputation: 11