Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Normally we can source ~/.bashrc file using this command

source ~/.bashrc

but if I write this in a shell script and execute it, nothing happen. Why?
Is there any way to do this?

my shell code:

#!/bin/bash
chmod a+x ~/.bashrc
source ~/.bashrc

Also tried .[dot] instead of source. Same result

share|improve this question
add comment

4 Answers 4

up vote 5 down vote accepted

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

alias brc='source ~/.bashrc'
share|improve this answer
    
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. –  shantanu Oct 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 Diesch Oct 5 '11 at 17:13
add comment

Try:

exec bash

This should reload ~/.bashrc, ~/.bash_aliases, etc.

share|improve this answer
    
thanks for your reply. It works but hang the process. –  shantanu Oct 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 Diesch Oct 5 '11 at 17:17
add comment

Your .bashrc usually starts:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

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.

share|improve this answer
add comment

I came up with this solution after running into this myself:

function re_source {
    xdotool type 'source ~/.bashrc'
    xdotool key Return
}

It works for me. You can call this from other bash functions. It is possibly the only way to "re_source" without invoking a new shell.

share|improve this answer
add comment

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.