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.

Possible Duplicate:
How can I make variables “exported” in a bash script stick around?

I have a problem with executing script from file. When I type in command line

PATH=$PATH:/home/

then PATH is changed appropriately. But when I execute this file :

#!/bin/sh
#provided by me

PATH=$PATH:/home/
echo "done"
exit 0

done is printed but PATH is not changed. Why is this happening ?

share|improve this question

marked as duplicate by Gilles, Mat, manatwork, Michael Mrozek Feb 23 '12 at 7:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

up vote 6 down vote accepted

Environmental variable changes apply to the current process and any subsequent children, but not to parent processes. So if you run a script, it cannot affect the environmental variables of the shell that ran it. You need to source the script using the . shell builtin. I.e.

. /path/to/script

This causes the current shell to execute the commands in the file instead of running a subprocess.

share|improve this answer
    
Can you tell me exactly how should I do this since now I am writing my script, putting it in /etc/init.d/ and running update-rc.d myScriptName defaults. Or in other words: is there better way to permanently change $PATH ? –  Patryk Feb 21 '12 at 0:19
2  
Set PATH in /etc/profile. This will affect sh, ksh and bash users. /etc/zprofile is the file to use for zsh users. –  Kyle Jones Feb 21 '12 at 0:41
    
I have tried source and . but it logs me off from chell. Why is this happening ? –  Patryk Feb 21 '12 at 13:09
1  
Remove the exit 0 from the script. It is telling your shell to exit, which is not what you want. –  Kyle Jones Feb 21 '12 at 17:29

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