4 Answers
4

A shell script is run in its own shell instance. All the variable settings, function definitions and such only affect this instance (and maybe its children) but not the calling shell so they are gone after the script finished.

By contrast the source command doesn't start a new shell instance but uses the current shell so the changes remain.

If you want a shortcut to read your .bashrc use a shell function or an alias instead of a shell script, like

Thanks for your quick reply. Your solution maybe work but i have to edit bashrc file manually to save the line 'aliac brc=....'. I am trying to develop a gui to change environment variable. So i can not edit another computer's bashrc file manually.
–
shantanuOct 5 '11 at 15:57

1

You have to run source ~/.bashrc in the shell of which you want to change the environment. You can not change it from another process. Maybe (globally) adding this alias could be a part of the install process of your GUI.
–
Florian DieschOct 5 '11 at 17:13

Since your script does not have PS1 set (because it is not interactive), it doesn't reset path because it exits early . To demonstrate, modify your script:

#!/bin/bash
chmod a+x ~/.bashrc
PS1='$ '
source ~/.bashrc

this will now allow your scripts to work with the new .bashrc.
Note: once your script exits , the env will be set to what it was before starting the script . The changes will be reflected the next time a terminal is started.

thanks for your reply. It works but hang the process.
–
shantanuOct 5 '11 at 15:55

This replaces the current bash process with a new one. It's not much shorter or easier than using source but destroys any variables and such that the user has set manually - which may or may not what you want.
–
Florian DieschOct 5 '11 at 17:17