5

Say this I have the following file my_cool_file.sh in a Git repository in Github (or BitBucket for that matter) named my_cool_repo. The file is a script used to install ConfigServer's well known CSF-LFD software:

#!/bin/bash
cd /usr/src
rm -fv csf.tgz
wget https://download.configserver.com/csf.tgz
tar -xzf csf.tgz
cd csf
sh install.sh
sed -i "s/TESTING = "1"/TESTING = "0"/g" /etc/csf/csf.conf
csf -r
perl /usr/local/csf/bin/csftest.pl
# sh /etc/csf/uninstall.sh

How can one execute this Bash script (a .sh file) directly from Github, via command line?

  • 1
    As you are putting this script up on github, consider #!/usr/bin/env bash as your hash-bang for better portability. – Mark Jan 5 '18 at 0:45
12

Load the file (make sure you use the raw file, otherwise you load the HTML page!) with wget using its exact URL and then pipe the output to bash:

Here's an example with clarifications:

wget -O - https://raw.githubusercontent.com/<username>/<project>/<branch>/<path>/<file> | bash

From the manpage for the wget command:

   -O file
   --output-document=file
       The documents will not be written to the appropriate files, but all
       will be concatenated together and written to file.  If - is used as
       file, documents will be printed to standard output, disabling link
       conversion.  (Use ./- to print to a file literally named -.)

       Use of -O is not intended to mean simply "use the name file instead
       of the one in the URL;" rather, it is analogous to shell
       redirection: wget -O file http://foo is intended to work like wget
       -O - http://foo > file; file will be truncated immediately, and all
       downloaded content will be written there.

So outputting to - will actually write the files content to STDOUT and then you simply pipe it to bash or whatever shell you prefer. If you script needs sudo rights you need to do sudo bash at the end so the line becomes:

wget -O - https://raw.githubusercontent.com/<username>/<project>/<branch>/<path>/<file> | sudo bash

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.