Navigating: Home » Bash » Sourcing With Bash Sourcing With Bash Posted on August 13, 2020August 16, 2020 by devsrealm Generally, to run a bash program or script you need to add a permission bit, which makes the program or script executable and you then run it whenever you like, the thing is when execution starts, a new process or a new sub-shell is opened in the background which runs each line of the scripts. Sourcing is also similar but slightly different, by sourcing, you are executing the file in the current shell itself, in the shell own process itself instead of in a new process, this is like typing each line of the script in at the command prompt one at a time, so the output of whatever you get when you are not sourcing or when you are running a script normally is not the output of the current shell but of the sub-shell, let’s take an example. Say you have a script that contains the following: #!/bin/bash a=1 So, I am basically declaring a variable and I set a value of 1, when I tried to echo the value of a in my shell using echo $a, I got nothing, now try running the script, and echo a using echo $a, you’ll still get nothing. why so? This is because when you run the script, it started a new sub-shell or a new process, and that new process assign a value of 1 to a, but as soon as we are done with the script, it takes us back to the original process. This is where sourcing is gonna be different, let’s try sourcing the script, to source a script you either use source script.sh or . script.sh, now if you echo the value of a, it is going to provide the value to us, e.g: user@server:~/bin$ source testsource.sh user@server:~/bin$ echo $a 1 That is all what sourcing is all about, it execute the file in the current shell, you can also use it to import variable and functions.