Search This Blog

2015-07-11

Sourcing Files in Bash

I never quite understood why when you changed .bashrc you had to type the source command in order to make changes active.

Now I understand and it's fairly straight forward.

Your current bash environment is defined by a PID that can be found using

echo $$


Any newly spawned bash processes for scripts will fork off of that process and be given a new process ID.

All variables for that newly spawned process will be local to that newly forked process.

If you would like the variables that would typically be local to your forked script to be active in your current shell you can use the source command in order to make them local to your current shell.

This acts in the same way when you would like to make variables in the .bashrc script local to your current shell. When the computer boots it runs .bashrc and puts variables in your current shell. If you would like to make any variable changes to re-read into your current shell. Similarly to how you would make variables to a remote script available to your local shell by using the source command you can do the same for .bashrc

Example:

source script.sh

The above will make all variables in script.sh available to your local shell at echo $$


source .bashrc

The above will make all variables in script .bashrc available to your local shell at echo $$

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.