amazon media file explorer download upload index folder image licenses menu widget Play Pause profile-settings more dots-two-horizontal dots-two-vertical more-vertical pending google-plus hangouts facebook instagram whatsapp spotify telegram twitter vine renren rss youtube twitch vimeo flickr dribble behance deviantart 500px steam github soundcloud skype reddit linkedin lastfm delicious stackoverflow pinterest xing flattr foursquare yelp World

Sourcing With Bash

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.

Related Post(s)

  • sed (Stream Editor) In Bash Scripting

    Another powerful filter in GNU/Linux is sed (stream editor), which is a program for performing basic editing tasks on the output of another program or on a file. sed or stream editor can be used to p

  • awk In Bash Scripting

    Up until now, we have covered a couple of text processing, from sed to grep to sort, and now we have the big daddy, which is awk. awk (which stands for Aho, Weinberger, and Kernighan - its creators)

  • (Bash Script) Automate Multiple Classicpress & Wordpress Installation + [Caching and SSL]

    If you are tired of using control panels (I am because they are mostly not secure, and most comes loaded with bundles of useless stacks), and want to do everything on the server level, I wrote a menu

  • Functions in Bash

    Functions are modular building blocks for creating powerful and modular scripts, using function makes it easy to isolate a code since it would be in a single building block, which would be isolated f

  • grep (Regular Expression) In Bash Scripting

    We recently discussed filters in bash scripting, in this guide we would look more into the practical usage of using a filter program, and an example of such a program is grep. grep, in its simplest f

  • Sort in Bash Scripting

    We recently just looked at grep in bash scripting, and another commonly used program in bash scripting is the program called sort. Sort is a simple filter that is used to sort lines of text from its