6

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.

3 Answers 3

10

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.

Sign up to request clarification or add additional context in comments.

2 Comments

can u tell me how to do the same in windows batch file
For batch file, i think that you can simply call the .bat file from parent file, as "initVars.bat". Make sure that it does not have any exit statement. Plus, to set a var, you need to use the command "set", like set VAR="SOME VALUE"
0

It should be

export VAR

Note the lack of the dollar sign symbol.

2 Comments

Are you running bash? Try this: echo $0 from the command prompt. If you don't see bash or sh, then it won't work.
@HaiVu It won't work for the reason in anishane's answer. export is POSIX, so it should work in sh as well.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.