sidharth sharma
sidharth sharma

Reputation: 3215

can seem to export environment variable from bash script

I am working on a remote server through putty and am trying to set certain environment variables throught something like this

#!/bin/bash
VAR="SOME VALUE"
export $VAR

when I exit the script and run echo on $VAR, I am given a blank line. could you please suggest a way around this.

Upvotes: 6

Views: 3797

Answers (3)

anishsane
anishsane

Reputation: 20970

First you need to export VAR, rather than $VAR.

Plus, I think, you are trying to execute the script from your shell as

./initVars.sh # or whatever is your script name...

You should rather source it as

source ./initVars.sh # OR
. ./initVars.sh # Note the leading '.' which serves as short-hand for source.

You can put it in your .bashrc.

Upvotes: 10

ezod
ezod

Reputation: 7411

Two problems. First, it's just export VAR. Second, $VAR will only be available to your script and child processes, not to the parent shell. You can source it like . ./yourscript.sh from the shell if you want $VAR to be set for that shell.

Upvotes: 0

Hai Vu
Hai Vu

Reputation: 40688

It should be

export VAR

Note the lack of the dollar sign symbol.

Upvotes: 0

Related Questions