# A Command A Day - source # The source Comand This will be a relatively short post, since the `source` command is pretty easy to use and there are really no options, but it's an important command to know and use in your daily sys admin or dev activities. The `source` command will execute commands line by line from a file in the current shell. If you give any arguments to source, they become the positional parameters within the file. ## Why Use source? Simple, current shell vs. new shell. Running a file with `./my-script` will open a new shell and all environment variables and commands will be executed within that shell. Once that script finishes, the shell will close and you will lose that session. Running that same script with `source ./my-script` will execute the commands in the file within the current shell, and any changes to the current shell will be retained once finished. This is especially useful with your bash profile and bashrc file when you add something like an export command or a change. Afterwards, you can use source to load that change into your current environment without having to do something like close your terminal and reopen it. ## Let's Take a Look Here is an example of source in action: We create a script with an env var in it. We source the script vs running it. ``` [jayson@RyterINC temp] ls source-example.sh [jayson@RyterINC temp] ./source-example.sh [jayson@RyterINC temp] env | grep myvar [jayson@RyterINC temp] [jayson@RyterINC temp] [jayson@RyterINC temp] source ./source-example.sh [jayson@RyterINC temp] [jayson@RyterINC temp] [jayson@RyterINC temp] env | grep myvar myvar=This is a test [jayson@RyterINC temp] ``` In the above example I create a script called `source-example.sh` and it has a single command: `export myvar="This is a test"` I make the script executable with `chmod +x ./source-example.sh` and I try to run the script with `./source-example.sh`. As you can see in the above example, I try and grep for the env var and I get no response. I then `source` the script, and in the following env command I am able to see that the env var has been exported in my session. ## In Conclusion Use `source`. It's easy to use, and can save you a bit of time and be helpful when managing your environment.