If you’re a seasoned Linux user, you may have used source command once or twice before. You might also have used ., the exactly same command, instead. Both source and . are shell builtin commands.

I have used this many times, but I only used for one purpose which is to read the environment variables from a file to the current shell environment. So, when I was asked during a job interview, “What does source do?”, the only answer I could tell them was - I personally only use source command to read the variables into the current shell environment.

So do you know “What does source do?”

According to its manpage (Manual), man . or man source, the . and source commands are shell built-in commands used to read and execute commands from filename in the current shell environment, and return the exit status of the last command executed from that filename.

For example, let’s assume that we want to check the value of a variable called DATABASE_URL.

➜ echo $DATABASE_URL

From the above output, we can see that value of DATABASE_URL is not yet set. Let’s create a file called .zsrhc.local with the following content:

DATABASE_URL=postgres://username:secret_password@sqlserver_address/dbname

Now, let’s source this .zshrc.local file, then check echo out the DATABASE_URL again.

➜ source .zshrc.local 
~
➜ echo $DATABASE_URL 
postgres://username:secret_password@sqlserver_address/dbname

Here is another example to show that if there are any commands in the file, they will also get executed when that file is sourced. Let’s update out .zshrc.local file to include a few commands:

DATABASE_URL=postgres://username:secret_password@sqlserver_address/dbname
TODAY_DATE=$(date)

cowsay ${TODAY_DATE}

Let’s try to source the file again.

➜ . ~/.zshrc.local
 __________________________________
< Mon Aug 30 07:01:57 PM AEST 2021 >
 ----------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Well, if you’ve read this blog post up to this point, I hope you’ve learned something today. If you didn’t know about the source command either, well, you’re probably like the majority of Linux users. There re so many things about Linux that we haven’t yet to uncover. Don’t put off by these overwhelming cool things about Linux that we’re yet to learn. :)

You can learn more about source from its awesome man page. Yes, you can run man . in your terminal for its manpage.