Updating or refreshing bash file on the fly

Today I learned a little trick about updating bash file on the fly. Earlier I was under impression every time you edit your bash or any other system related shell script file, it is necessary to restart the terminal. However, I realized that this is not the case.

With the following trick, it's ok to run a single command rather than having to update bash file and restarting terminal.

Let's take an example. Say you want to add an environment variable to your .bash_profile file. You type the following command in the terminal,

vi ~/.bash_profile

And add following line to the file,

export USERNAME="jayesh"

Newly included environment variable can be accessed using ENV["USERNAME"]in any script

Save and exit the file. Now type printenv on the command line. This command will print all the environment variable defined in the system.

As you will notice, newly added environment variable won't be printed as an output. This is because system hasn't refreshed yet to reflect the newly added system parameter. Now type either of the following commands in the terminal.

. ~/.bash_profile
#or
source ~/.bash_profile

And now see the magic by typing printenv one more time to see all the environment variables. You will see that newly added environment variable has been successfully added to the system without restarting a terminal.