Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have multiple Amazon EC2 accounts and want to quickly be able to switch variables, such as $EC2_HOME, using a script.

I have have a shell script set up like this:

#!/bin/sh
export EC2_HOME=/home/me/.ec2
echo $EC2_HOME

When I run the script I know that EC2_HOME is set, but I thought that using export would make the variable stick around after the script completed. It does not, as running echo $EC_HOME does not show anything.

I know this must be very rudimentary Linux scripting knowledge, but I don't know it. I tried looking for related questions without luck - so my apologies if this is a duplicate.

share|improve this question
    
First... remove the spaces between the var and the value: EC2_HOME=/home/me/.ec2 –  gabe. Jan 27 '12 at 19:39
    
thanks i think it didn't paste in right. –  cwd Jan 27 '12 at 19:39

1 Answer 1

up vote 10 down vote accepted

You should source your script, with

. ./script

or

source ./script
share|improve this answer
6  
the reason is that your script spawns a new shell process as a child of the current shell. Any environment changes you make in the child process cannot affect the parent. When you use . or source, you are not spawning a new child process, you are running the commands in the current shell. –  glenn jackman Jan 27 '12 at 20:32
    
@glennjackman I have a similar problem and I have tried your solution but it logs me off from shell when I do . or source. Why is this happening ? –  Patryk Feb 21 '12 at 13:03
3  
@Patryk: maybe your script has an exit statement, so it is not suitable to be sourced. –  enzotib Feb 21 '12 at 13:24

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.