DEV Community

killDevils
killDevils

Posted on

Bash | How to source a file from GitHub?

When you are invoking BootStrap in HTML file, don't bother to download related files, just paste the link:

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

Often, for future convenience, I gather all Bash functions created by myself in a single file named funkfunc.sh, so I can source it whenever I need. But, in different projects, I still have to copy the file respectively. In most cases, the hosts are allowed to access Internet, why don't make an online version of funkfunc.sh?

Source a file from a public repo in GitHub

For example, I have a public repo: killDevils/PublicSource, there is a file named "bashKit.sh". When I view the "Raw" file of it, the Browser shows me the link: https://raw.githubusercontent.com/killDevils/PublicSource/master/bashKit.sh

Now, add a line at the top of .sh file (below shebang tag #!/bin/bash):

source <(curl -s https://raw.githubusercontent.com/killDevils/PublicSource/master/bashKit.sh)

Source a file from a PRIVATE repo in GitHub

First, please read my old post:GitHub | Download a file from a Private Repository in GitHub, get to know how to generate the link we need.

For example, The link I got is https://1234abc45678cde6543edca@raw.githubusercontent.com/killDevils/PrivateRepo/master/funkfunc.sh

Now, add a line at the top of .sh file (below shebang tag #!/bin/bash):

source <(curl -s https://1234abc45678cde6543edca@raw.githubusercontent.com/killDevils/PrivateRepo/master/funkfunc.sh)

🍻

Hope this would help! Cheers!

Top comments (0)