Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

chmod a+x ~/.bashrc
source ~/.bashrc

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

share|improve this question
up vote 9 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
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
so do i put your alias command earlier in the script and then invoke brc when i want to source my .bashrc or I need to put that alias command in a file somewhere? – user137717 Jun 25 at 19:08


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

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:

    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

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
I don't think xdotools is available by default – Felipe Almeida Jan 25 at 0:17
I dare say it isn't available, ... by default. – Cbhihe Mar 28 at 18:42
I came up with this horrible "solution" many years ago. I ended up using an alias: alias .a='source ~/.bash_aliases'. – user84207 Mar 28 at 20:21

Your Answer


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.