How to execute a Bash script from Github?

23,113

Solution 1

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

Solution 2

If your script has user entries like read user_input try this:

bash <(curl -s https://raw.githubusercontent.com/username/project/branch/path/file.sh)

The -s parameter don't show download progress.

Share:
23,113
Arcticooling
Author by

Arcticooling

Updated on September 18, 2022

Comments

  • Arcticooling
    Arcticooling over 1 year

    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?

    • Mark
      Mark over 6 years
      As you are putting this script up on github, consider #!/usr/bin/env bash as your hash-bang for better portability.
  • anarchist912
    anarchist912 over 4 years
    What about params? Let's say the piped script optionally handles $1, would there be a way to pass it in?